On Tue, 07 Sep 2004 12:30:30 +0200, Erwin Moller
<si******************************************@spam yourself.com> wrote:
Michael Winter wrote:
On Tue, 07 Sep 2004 08:27:11 GMT, Terry Olson <tw******@hotmail.com>
wrote:
I am using Regular Expressions, I tried putting just a simple \ in
front of it; as in /\$/g and that seems to work, anyone got a better
idea?
That *is* how to do it. The only way, in fact. :)
[snip]
$str1 = '$Hello! All the dollars $ should $ be replaced by $ **';
$str2 = ereg_replace ( '\$', '**', $str1);
If converted to Javascript, you don't need the backslash with the string
argument form of the replace method. In that instance, it's a simple
string comparison. If you are using a regular expression, then the
backslash is necessary to ensure the ampersand isn't interpreted as a
special character. This latter case is what I was referring to. So
initially, your two options are:
var str = '$Hello! All the dollars $ should $ be replaced by $ **',
res;
res = str.replace('$', '**');
or
res = str.replace(/\$/, '**');
However, both of these should only replace the first instance of an
ampersand. If all occurances need to be replaced, you have to use regular
expression the OP presented:
res = str.replace(/\$/g, '**');
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.