473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Finding a number in string - RegExp

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
6 3099
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
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
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
Bingo!
The double backslashes made the trick!

Thanks a lot! :)
Nick

Aug 13 '05 #5
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Eric Linders | last post by:
Hi, I'm trying to figure out the most efficient method for taking the first character in a string (which will be a number), and use it as a variable to check to see if the other numbers in the...
5
by: lawrence | last post by:
"Garp" <garp7@no7.blueyonder.co.uk> wrote in message news:<_vpuc.1424$j_3.13346038@news-text.cableinet.net>... > "lawrence" <lkrubner@geocities.com> wrote in message >...
1
by: Tristan | last post by:
Im trying to expand a search util by uing regular expression to allow common search criteria such as +-* and phrases "". My understanding of ereg(string pattern, string string, ) is that the...
3
by: Jacob Weber | last post by:
If I do a search with a regular expression that uses parenthesized subexpressions, is there a way to find out where it found the subexpressions? For example, say I do: "bb".search(/b(.)/g); ...
7
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For...
4
by: possibilitybox | last post by:
I'm trying to make a unicode friendly regexp to grab sentences reasonably reliably for as many unicode languages as possible, focusing on european languages first, hence it'd be useful to be able...
2
by: ElkGroveR | last post by:
Hi there! I'm using PHP to create a simple, dynamic MySQL SELECT query. The user chooses a selection from a HTML Form SELECT element's many options and submits the form via a POST action. ...
7
by: gtb | last post by:
I wish to copy the highest version number of a file from directory \ \ \fileserver\D:\scripts to C:\scripts where the file names are of the form filename_MM.NN.SS.zip, where MM, NN, and SS...
2
by: pnsreee | last post by:
Hi All, I have the following strings seperated by space . I have to grep for Naveen having two values(Sha or See) in the string. 1)Naveen Sha reswww 2)Naveen See rex-www...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.