473,799 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3121
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.javas cript 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*******@hotm ail.com> wrote in message news:11******** *************@g 44g2000cwa.goog legroups.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************ *********@g44g2 000cwa.googlegr oups.com>,
dated Sat, 13 Aug 2005 14:46:47, seen in news:comp.lang. javascript, Nick
<ni*******@hotm ail.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.demo n.co.uk/js-valid.htm>.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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************ *********@g44g2 000cwa.googlegr oups.com>,
dated Sat, 13 Aug 2005 14:46:47, seen in news:comp.lang. javascript, Nick
<ni*******@hotm ail.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.javas cript 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
6987
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 string match that first number. I'm using this code for form validation of a telephone number. Previous records from the past few months show that when someone is just messing around on one of our forms (to waste our time), they type
5
2527
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 > news:da7e68e8.0405300907.3c8eabf7@posting.google.com... > > This reg ex will find me any block of strings 40 or more characters in > > length without a white space, yes? > > > > {40} > > > > > > To get it to include tabs and newlines, do I to this?
1
2079
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 array register should collect all instances that match the pattern. Heres is an example of the code: $word4 = "+budgie +ferret dog."; //set phrase
3
1510
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); Those parentheses matched the second "b", at index 1. Is there any way to find that out?
7
3452
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 example: var re = /some(thing|or other)?.*(n(est)(?:ed)?.*(parens) )/ var text = "There were some nesting parens in the test"; alert (regExpPos (text, re, 3));
4
3104
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 to refer to any uppercase unicode character instead of just the typical , which doesn't include, for example É. Is there a way to do this, or do I have to stick with using the isupper method of the string class?
2
2853
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. The SELECT query is built as follows: $itemtype = stripslashes(trim($_POST));
7
2195
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 can be one to three digits. Example directory: other.zip
2
1466
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 3)Naveen See rex.x 4)Naveen xxxx cccccc 5)ABBCD See reggg I have the following regexp.
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9543
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10029
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.