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

Finding a number in string - RegExp

P: n/a
Hi,

How can I check if a number exists by itself in this string by using
the RegExp object?

---

var mystring = "11,111,01,011";
var match = "1";
var re = new RegExp( match );
var isFound = re.test( mystring ) );

---

Running this code returns 'true' which is not what I want since number
one doesn't exist by itself. I need to use the "match" variable since
it will change depending on user input.

I can only get it to work without variables in the expression. E.g.

var re = new RegExp( /\b1\b/ );

Many thanks

Aug 13 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Nick said the following on 8/13/2005 5:46 PM:
Hi,

How can I check if a number exists by itself in this string by using
the RegExp object?

---

var mystring = "11,111,01,011";
var match = "1";
var re = new RegExp( match );
var isFound = re.test( mystring ) );

---

Running this code returns 'true' which is not what I want since number
one doesn't exist by itself. I need to use the "match" variable since
it will change depending on user input.

I can only get it to work without variables in the expression. E.g.

var re = new RegExp( /\b1\b/ );

Many thanks


var mystring = "11,111,01,011";
var bound1 = "\b"
var bound2 = "\b"
var match = "1"
var re = new RegExp(bound1 + match + bound2);
var isFound = re.test( mystring );
alert(isFound)
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Aug 13 '05 #2

P: n/a
Thanks Randy,
However, this code doesn't seem to produce the correct result if I for
example set match to "11" instead.
It returns false no matter what I set. Any ideas?

Aug 13 '05 #3

P: n/a
Nick <ni*******@hotmail.com> wrote in message news:11*********************@g44g2000cwa.googlegro ups.com...
Hi,

How can I check if a number exists by itself in this string by using
the RegExp object?

---

var mystring = "11,111,01,011";
var match = "1";
var re = new RegExp( match );
var isFound = re.test( mystring ) );

---

Running this code returns 'true' which is not what I want since number
one doesn't exist by itself. I need to use the "match" variable since
it will change depending on user input.

I can only get it to work without variables in the expression. E.g.

var re = new RegExp( /\b1\b/ );

Many thanks


Try:

var match = '\\b1\\b';
var re = new RegExp( match );

--
Stephen Chalmers
Aug 13 '05 #4

P: n/a
Bingo!
The double backslashes made the trick!

Thanks a lot! :)
Nick

Aug 13 '05 #5

P: n/a
JRS: In article <11*********************@g44g2000cwa.googlegroups. com>,
dated Sat, 13 Aug 2005 14:46:47, seen in news:comp.lang.javascript, Nick
<ni*******@hotmail.com> posted :
How can I check if a number exists by itself in this string by using
the RegExp object?

You need to define "number" and "by itself" unambiguously; only then can
a suitable RegExp be chosen.

Does "number" mean digit, decimal digit, one or more of those, with a
sign, with a decimal point, with an exponent part, octal or hexadecimal
number, within the range of type Number ??

Does "by itself" mean that there must be no other characters in the
string, no other "printing" characters?; or is it only the characters on
each side that matter?; what if it is at the beginning or end of the
string?

If it is the characters on each side that matter, which are allowed?
The numbers of the properties to the east of to mine are of the form 97X
& 97Y.

Is the 5 in a5a "by itself"? Is punctuation allowed, but not letters?
How about .5 - is that a half or a five?

<URL:http://www.merlyn.demon.co.uk/js-valid.htm>.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 14 '05 #6

P: n/a
Dr John Stockton said the following on 8/14/2005 9:40 AM:
JRS: In article <11*********************@g44g2000cwa.googlegroups. com>,
dated Sat, 13 Aug 2005 14:46:47, seen in news:comp.lang.javascript, Nick
<ni*******@hotmail.com> posted :

How can I check if a number exists by itself in this string by using
the RegExp object?
You need to define "number" and "by itself" unambiguously; only then can
a suitable RegExp be chosen.


Those two seem to be self definitive to anyone who read the post other
than you.
Does "number" mean digit, decimal digit, one or more of those, with a
sign, with a decimal point, with an exponent part, octal or hexadecimal
number, within the range of type Number ??
From the example, and following posts, that again is self-evident to
anyone but you.
Does "by itself" mean that there must be no other characters in the
string, no other "printing" characters?; or is it only the characters on
each side that matter?; what if it is at the beginning or end of the
string?
<sigh> Again, that was self-evident. He wanted to find 1, but not 11 or
a1a or any other form, he wanted 1 and only 1. That was obvious to
anyone but you.
If it is the characters on each side that matter, which are allowed?
Read above. You seem to enjoy trying to make things harder than they are.
The numbers of the properties to the east of to mine are of the form 97X
& 97Y.

Is the 5 in a5a "by itself"?
Only to someone like you.
Is punctuation allowed, but not letters?
Geez, you just don't get it do you?
How about .5 - is that a half or a five?


Neither, it is the numeral five preceded by a decimal. Whether that is
half, five, or something else is entirely dependent on the base in use.
..5 in base 8 is neither Five nor Half. In fact, it is neither in any
base above 5 but not 10.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Aug 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.