Finding a number in string - RegExp

N

Nick

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
 
R

Randy Webb

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)
 
N

Nick

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?
 
S

Stephen Chalmers

Nick said:
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 );
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Sat, 13 Aug 2005 14:46:47, seen in Nick
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>.
 
R

Randy Webb

Dr John Stockton said the following on 8/14/2005 9:40 AM:
JRS: In article <[email protected]>,
dated Sat, 13 Aug 2005 14:46:47, seen in Nick




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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top