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

RegExp can't find a period

P: n/a
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
Share this Question
Share on Google+
3 Replies


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

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

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