How to replace ^ with an other character in a string ?

A

ast

hi

I am trying to replace the character ^ in a string with character %
using "replace" string method. I know that ^ is a special character
so it has to be backslashed. But it doesnt work.

example

<html>
<body>
<script type="text/javascript">

var s = "x^12+2";
var s2 = "";

var reg = new RegExp("\^", "g");

s2 = s.replace(reg, "%" );

document.write(s2);

</script>
</body>
</html>

It provide %x^12+2 instead of x%12+2
I tried many variations

var reg = new RegExp("^", "g");
var reg = new RegExp("/^", "g");
var reg = new RegExp('^', "g");

but nothing works.
Same behavior with IE8 and firefox 3.6.8

Any idea ?
 
A

ast

var p = s.indexOf('^');
if (p>=0) s2 = s.substr(0,p-1)+'%'+s.substr(p+1);
else s2 = s;

yes, but what i really want to do is more complex. So i need
to use the "replace" method.
 
A

ast

"Jake Jarvis" <[email protected]> a écrit dans le message de

It should have been

new RegExp("\\^", "g");

\ has special meaning in string literals already, in order
to have it in the 'end string' you must escape it.

yes it works thanks. but I dont understand why.

for example, /a*/ means match 0 or more a's. To match * literally,
precede it with a backslash; for example, /a\*/ matches 'a*'.

why do i need 2 backslash to espace ^ and only one to escape * ?
 
A

ast

ast said:
"Jake Jarvis" <[email protected]> a écrit dans le message de



yes it works thanks. but I dont understand why.

for example, /a*/ means match 0 or more a's. To match * literally,
precede it with a backslash; for example, /a\*/ matches 'a*'.

why do i need 2 backslash to espace ^ and only one to escape * ?


I found answer:

Also do not forget to escape \ itself while using the new RegExp("pattern")
notation since \ is also an escape character in strings.

so we have to write:

reg = /\^/;
or
reg = new RegExp("\\^", "g");
 
T

Thomas 'PointedEars' Lahn

ast said:
| Also do not forget to escape \ itself while using the new
| RegExp("pattern") notation since \ is also an escape character
| in strings.

so we have to write:

reg = /\^/;
or
reg = new RegExp("\\^", "g");

No. Either

reg = /\^/g;

or

reg = new RegExp("\\^", "g");

Or

reg = /\^/;

or

reg = new RegExp("\\^");


PointedEars
 
R

Ry Nohryb

hi

I am trying to replace the character ^ in a string with character %
using "replace" string method. I know that ^ is a special character
so it has to be backslashed. But it doesnt work. (...)

"x^12+2^2".replace(/\^/g, '%');
-> "x%12+2%2"
 
R

Ry Nohryb

"x^12+2^2".replace(/\^/g, '%');
-> "x%12+2%2"

Also, for more complicated transformations you can pass a function as
the 2nd argument to .replace():

var ordinal= 0;
function Æ’ (char, pos) {
ordinal++;
var txt= "[ordinal:"+ ordinal;
txt+= ", pos:"+pos;
txt+= ", char:"+ char
txt+= "]";
return txt;
}

"x^12+2^2".replace( /\^/g, Æ’ )
-> "x[ordinal:1, pos:1, char:^]12+2[ordinal:2, pos:6, char:^]2"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top