By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Search and Replace for "$" in text

P: n/a
I want to search a textbox and replace any instances of the dollar sign
character. But when I do that it thinks it won't work because it think's I'm
looking to replace something at the end of the line. How do I tell it to
look for literal dollar signs?
Jul 23 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Terry Olson wrote:
I want to search a textbox and replace any instances of the dollar sign
character. But when I do that it thinks it won't work because it think's
I'm looking to replace something at the end of the line.
What is 'it'?
Some mysterycode we can only guess about?
How do I tell it
to look for literal dollar signs?


2 things that pop into mind:
1) use String.substring() in combination with indexOf() and some basic
coding.
2) Or a regular expression which is probably more complex to understand for
starters.
I would advise you to do it with the first option.

Good luck.

Erwin Moller

Jul 23 '05 #2

P: n/a
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?

"Erwin Moller"
<si******************************************@spam yourself.com> wrote in
message news:41***********************@news.xs4all.nl...
Terry Olson wrote:
I want to search a textbox and replace any instances of the dollar sign
character. But when I do that it thinks it won't work because it think's
I'm looking to replace something at the end of the line.
What is 'it'?
Some mysterycode we can only guess about?
How do I tell it
to look for literal dollar signs?


2 things that pop into mind:
1) use String.substring() in combination with indexOf() and some basic
coding.
2) Or a regular expression which is probably more complex to understand

for starters.
I would advise you to do it with the first option.

Good luck.

Erwin Moller

Jul 23 '05 #3

P: n/a
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. :)

Mike
Please don't top-post.

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4

P: n/a
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. :)

Mike
Please don't top-post.


This works fine for me:

$str1 = '$Hello! All the dollars $ should $ be replaced by $ **';
$str2 = ereg_replace ( '\$', '**', $str1);

echo "str1= $str1<br>";
echo "str2= $str2<br>";
Jul 23 '05 #5

P: n/a
Erwin Moller 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. :)

Mike
Please don't top-post.


This works fine for me:

$str1 = '$Hello! All the dollars $ should $ be replaced by $ **';
$str2 = ereg_replace ( '\$', '**', $str1);

echo "str1= $str1<br>";
echo "str2= $str2<br>";


Oops, sorry, that was PHP of course. :-)
Excuse me.
Jul 23 '05 #6

P: n/a
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.
Jul 23 '05 #7

P: n/a
Michael Winter wrote on 07 sep 2004 in comp.lang.javascript:
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:


I know the $ is devaluating, but has it become an & ?

;-}

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #8

P: n/a
On 07 Sep 2004 13:10:09 GMT, Evertjan. <ex**************@interxnl.net>
wrote:
Michael Winter wrote on 07 sep 2004 in comp.lang.javascript:
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:


I know the $ is devaluating, but has it become an & ?

;-}


:P

Hmm. Yes, I meant a dollar symbol rather than an ampersand.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #9

P: n/a
JRS: In article <opsdx7e9d6x13kvk@atlantis>, dated Tue, 7 Sep 2004
08:33:02, seen in news:comp.lang.javascript, Michael Winter <M.Winter@bl
ueyonder.co.invalid> posted :
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. :)


x = "34$56".replace(/\x24/, '#')
x = "34$56".replace(/\044/, '#')
x = "34$56".replace(/\u0024/, '#')
x = "34$56".replace(/[^\000-#%-\377]/, '#')

all give me "34#56".

By "only", you must mean "proper" or "best" rather than "unique".

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links;
some Astro stuff via astro.htm, gravity0.htm; quotes.htm; pascal.htm; &c, &c.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Jul 23 '05 #10

P: n/a
On Tue, 7 Sep 2004 20:42:42 +0100, Dr John Stockton
<sp**@merlyn.demon.co.uk> wrote:
JRS: In article <opsdx7e9d6x13kvk@atlantis>, dated Tue, 7 Sep 2004
08:33:02, seen in news:comp.lang.javascript, Michael Winter
<M.******@blueyonder.co.invalid> posted :
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. :)


x = "34$56".replace(/\x24/, '#')
x = "34$56".replace(/\044/, '#')
x = "34$56".replace(/\u0024/, '#')
x = "34$56".replace(/[^\000-#%-\377]/, '#')

all give me "34#56".

By "only", you must mean "proper" or "best" rather than "unique".


The OP wanted to know if the backslash was necessary. I took that to mean,
"Is escaping necessary?". My reply was yes.

I must admit, I hadn't considered the last approach, but I think that
one's just a little over-the-top. :)

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.