JS regex not replacing with dollar symbols correctly

F

Flintstone

I am trying to replace particular words in a string with a sequence of
dollar signs (necessary for a JS FTP app which is part of a larger
project).

The following code should replace test_dollar_test with test_$$$$$
$_test but it only replaces with test_$$$_test. Sure, I can just use \$
\$\$\$\$\$\$\$\$\$\$\$ but then if this is a bug and they fix it my
app will not work. Does anybody know why this happens? Any workarounds
are welcome.

<html>
<head>
<title>test</title>
<script language="javascript">
var str = 'test_dollar_test';
str = str.replace(/dollar/g, "\$\$\$\$\$\$");
alert(str);
</script>
</head>
<body>

</body>
</html>
 
E

Evertjan.

Flintstone wrote on 22 aug 2007 in comp.lang.javascript:
var str = 'test_dollar_test';
str = str.replace(/dollar/g, "\$\$\$\$\$\$");
alert(str);

var str = 'test_dollar_test';
str = str.replace(/dollar/g,'$$$$$$$$$$$$');
alert(str);

For a better value, use euros '€€€€€€', Fred.
 
S

Stevo

Flintstone said:
I am trying to replace particular words in a string with a sequence of
dollar signs. Sure, I can just use \$\$\$\$\$\$\$\$\$\$\$\$
but then if this is a bug and they fix it my app will not work.
str = str.replace(/dollar/g, "\$\$\$\$\$\$");

If "they" fixed/changed anything, I very much doubt it would stop your
backslash escaping method from working. I prefer that to what Evertjan
suggested (using 12 $ symbols).
 
T

Thomas 'PointedEars' Lahn

Flintstone said:
I am trying to replace particular words in a string with a sequence of
dollar signs (necessary for a JS FTP app which is part of a larger
project).

The following code should replace test_dollar_test with test_$$$$$
$_test but it only replaces with test_$$$_test. Sure, I can just use \$
\$\$\$\$\$\$\$\$\$\$\$ but then if this is a bug

It's not a bug, it's a feature :)
and they fix it my app will not work. Does anybody know why this happens?

`\$' is not a valid escape sequence, so it is parsed as if it was only `$':

[ES3], 7.8.4
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Unicode#Unicode_escape_sequences
http://msdn2.microsoft.com/en-us/library/2yfce773.aspx

And `$$' means a literal `$':

[ES3], 15.5.4.11
http://developer.mozilla.org/en/doc...ng:replace#Specifying_a_string_as_a_parameter
http://msdn2.microsoft.com/en-us/library/t0kbytzc.aspx
Any workarounds are welcome.

<html>

There is not need to post (not Valid) HTML here if your code does not have
anything to do with it (see said:
[...]
var str = 'test_dollar_test';
str = str.replace(/dollar/g, "\$\$\$\$\$\$");

// 11 $s suffice, but 12 are more reliable
str = str.replace(/dollar/g, "$$$$$$$$$$$$");

or

str = str.replace(/dollar/g, function() { return "$$$$$$"; });


HTH

PointedEars
___________
[ES3]
http://developer.mozilla.org/en/docs/JavaScript_Language_Resources#JavaScript_1.x
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
oglegroups.com>, Wed, 22 Aug 2007 07:20:06, Flintstone
I am trying to replace particular words in a string with a sequence of
dollar signs (necessary for a JS FTP app which is part of a larger
project).

str = str.split("dollar").join("$$$$$$")

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top