469,351 Members | 1,814 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,351 developers. It's quick & easy.

RegExp can't find a period

Is there any reason why RegExp wouldn't be able to find a period?

str = '256.89';
a = new RegExp('(\.)');
b = /(\.)/;
alert(str.replace(a, 'x$1x'));
alert(str.replace(b, 'x$1x'));

The alert for a says
x2x56.89

However the alert for b says
256x.x89

b is the result I want, because it means that the period was found. a
is just finding a single character, as if it's ignoring the escape in
front of the period.

I've tried this in IE and Mozilla.
Jul 20 '05 #1
3 3071
On 15 Jan 2004 01:01:23 -0800, tr**********@yahoo.com (trialofmiles)
wrote:
Is there any reason why RegExp wouldn't be able to find a period?

str = '256.89';
a = new RegExp('(\.)');
Here's a hint:
When used in a STRING, the backslash is the escape character.
b = /(\.)/;
alert(str.replace(a, 'x$1x'));
alert(str.replace(b, 'x$1x'));

The alert for a says
x2x56.89

However the alert for b says
256x.x89

b is the result I want, because it means that the period was found. a
is just finding a single character, as if it's ignoring the escape in
front of the period.

I've tried this in IE and Mozilla.


Regards,
Steve
Jul 20 '05 #2
On Thu, 15 Jan 2004 11:14:05 GMT, Steve van Dongen
<st*****@hotmail.com> wrote:
On 15 Jan 2004 01:01:23 -0800, tr**********@yahoo.com (trialofmiles)
wrote:
Is there any reason why RegExp wouldn't be able to find a period?

str = '256.89';
a = new RegExp('(\.)');


Here's a hint:
When used in a STRING, the backslash is the escape character.


Yeah, what you actually need to do is:

a = new RegExp('(\\.)');

when its a string.

hth
Al
Jul 20 '05 #3
Steve van Dongen <st*****@hotmail.com> wrote in message news:<hc********************************@4ax.com>. ..
Here's a hint:
When used in a STRING, the backslash is the escape character.


Sometimes the solution is so simple. Thank you very much.

What's strange is at one point I had written it \\. and things weren't
working the way I expected. But that was when I was trying it as part
of a larger regular expression. Another piece of the expression must
have been wrong. Things are working now.
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Greg Bryant | last post: by
10 posts views Thread by Anand Pillai | last post: by
5 posts views Thread by Lukas Holcik | last post: by
10 posts views Thread by Andrew DeFaria | last post: by
10 posts views Thread by Jeff Sandler | last post: by
reply views Thread by Chris Croughton | last post: by
2 posts views Thread by Nathan Sokalski | last post: by
4 posts views Thread by Matt | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.