Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-15-2008, 07:05 PM   PM User | #1
rhyno
New Coder

 
Join Date: Sep 2007
Posts: 21
Thanks: 3
Thanked 0 Times in 0 Posts
rhyno is an unknown quantity at this point
Regular Expression Woes - matching number patterns

Hi,


I've been trying for many hours to match simple number patterns.

In a nutshell, I want to match every 5- or 6-digit number preceded by either whitespace or #(pound sign).
I do not want to match numbers 7-digits or more.

Here's what I have so far:
matches = notestxt.match(/(?!#|\D)[0-9]{5,6}(?!\d)/ig);
where notestxt is the content of the DIV I'm processing.

It matches 5 & digit strings OK, but also catches a 8-digit strings (10046512, e.g.).

What's worse, in IE, matches are highlighted, plus the entire DIV is duplicated! ... and preceded by an extraneous ">

Demo here:
http://jsbin.com/utuva
Code:
http://jsbin.com/utuva/edit (click JavaScript tab)

What's going on here?


Thanks,
--RHYNO
rhyno is offline   Reply With Quote
Old 10-15-2008, 07:35 PM   PM User | #2
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
would
matches = notestxt.match(/[\s\#]\d{5,6}?/g);

work?
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 10-15-2008, 07:46 PM   PM User | #3
rhyno
New Coder

 
Join Date: Sep 2007
Posts: 21
Thanks: 3
Thanked 0 Times in 0 Posts
rhyno is an unknown quantity at this point
rnd_me,

Thanks for the suggestion, but that seems to be capturing the whitespace or # signs as well (I just want to capture the digits). The 8-digit numbers are having their first 5-digits captured with preceding whitespace as well.

What I'm after is:

before matching:

Lorem ipsum dolor sit amet 09999 #08220 #082201, 082202 consectetuer adipiscing elit. Ut eleifend ipsum nec risus. Proin arcu ligula, part# 10046512 hendrerit et, fringilla a, dapibus sed, libero.

after matching:

Lorem ipsum dolor sit amet 09999 #08220 #082201, 082202 consectetuer adipiscing elit. Ut eleifend ipsum nec risus. Proin arcu ligula, part# 10046512 hendrerit et, fringilla a, dapibus sed, libero.

(adding hyperlinks to matched number strings)
rhyno is offline   Reply With Quote
Old 10-15-2008, 07:50 PM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
ahh.

to stop the 8 digit mismatch:
matches = notestxt.match(/[\s\#]\d{5,6}?\s/g);

now you still have junk left on the end of the number.
while you can mess around with non-caputuring parens, i think it's easier and faster to simply run an extra replace inline.

ex: str= str.replace(/\D/g,"") would kill all the non digits.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Users who have thanked rnd me for this post:
rhyno (10-15-2008)
Old 10-15-2008, 08:52 PM   PM User | #5
rhyno
New Coder

 
Join Date: Sep 2007
Posts: 21
Thanks: 3
Thanked 0 Times in 0 Posts
rhyno is an unknown quantity at this point
Ahh, great... just about there!

matches = notestxt.match(/[\s\#]\d{5,6}?[\s,]/g);
works great (catching commas after string as well).

I'm able to strip out non-digits with the .replace() too... but only in Firefox.

Naturally, IE7 triggers an "object doesn't support this property or method" error on the:
str = str.replace(/\D/g, "");
line.

Googling it now, but there doesn't seem to be a clear IE/.replace() JS issue?
rhyno is offline   Reply With Quote
Old 10-15-2008, 08:54 PM   PM User | #6
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
If you have a string that can have any number of matches, and you want to return the matches in an array, you can step through the string with a regular expression exec-
Code:
var A=[], M;
while((M=/[\s#](\d{5,6})(?!\d)/g.exec(notestxt))!=null){
    A[A.length]=M[1];
}
// A now contains every 5 or 6 digit integer that follows a # or whitespace in notestxt

If you have more than one match, replacing the non-digits with the empty string will turn your matches into a single long string of digits, which won't match-
if you go that route, replace non-digits (\D+) with a space instead.

Last edited by mrhoo; 10-15-2008 at 09:04 PM..
mrhoo is offline   Reply With Quote
Old 10-15-2008, 09:41 PM   PM User | #7
rhyno
New Coder

 
Join Date: Sep 2007
Posts: 21
Thanks: 3
Thanked 0 Times in 0 Posts
rhyno is an unknown quantity at this point
Found the IE issue.

It was barking about the .replace() line, but in reality it had a problem with my for() loop shortcut:

for (m in matches) { ... } // IE no likey

for (var i = 0; i < matches.length; i++) { ... } // OK, go ahead

Sheesh!

Thank you rnd_me for the assistance, and mrhoo, I might try your method in the future.


--RHYNO
rhyno is offline   Reply With Quote
Old 10-15-2008, 10:21 PM   PM User | #8
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by rhyno View Post
Found the IE issue.

It was barking about the .replace() line, but in reality it had a problem with my for() loop shortcut:

for (m in matches) { ... } // IE no likey

for (var i = 0; i < matches.length; i++) { ... } // OK, go ahead

Sheesh!

Thank you rnd_me for the assistance, and mrhoo, I might try your method in the future.


--RHYNO
yeah, replace works only on single matches...

glad you got it working.

-cheers!
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 10-16-2008, 01:01 PM   PM User | #9
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
mrhoo's script caused my browser to lock up.

To summarize, the solution to the problem is:-

Code:
<script type = "text/javascript">

var notestxt = "Lorem ipsum dolor sit amet 09999 #08220 #082201, 082202 consectetuer adipiscing elit. Ut eleifend ipsum nec risus. Proin arcu ligula, part# 10046512 hendrerit et, fringilla a, dapibus sed, libero."

notestxt1 = notestxt.replace(/\D/g, " ");
matches = notestxt1.match(/\s\d{5,6}\s/g);
alert (matches);

</script>

I have nothing but confidence in you. And very little of that.
Groucho Marx (1890 - 1977)

Last edited by Philip M; 10-16-2008 at 01:03 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
mrhoo (10-16-2008)
Old 10-16-2008, 02:21 PM   PM User | #10
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
By putting a regular expression literal in the loop I was calling for a new RegExp in each iteration-
the lastIndex continually gets set to 0, instead of after the last match.

Defining the RegExp outside of the loop solves it.
Thank you for catching it, Philip.

var notestxt = "Lorem ipsum dolor sit amet 09999 #08220 #082201, 082202 consectetuer adipiscing elit. Ut eleifend ipsum nec risus. Proin arcu ligula, part# 10046512 hendrerit et, fringilla a, dapibus sed, libero."


Code:
var A=[], M;
var Rx=/[\s#](\d{5,6})(?!\d)/g;
while((M=Rx.exec(notestxt))!=null){
    A[A.length]=M[1];
}
alert(A) // returns [09999,08220,082201,082202]

Last edited by mrhoo; 10-16-2008 at 02:26 PM..
mrhoo is offline   Reply With Quote
Old 10-16-2008, 06:11 PM   PM User | #11
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by mrhoo View Post
By putting a regular expression literal in the loop I was calling for a new RegExp in each iteration-
the lastIndex continually gets set to 0, instead of after the last match.

Defining the RegExp outside of the loop solves it.
Thank you for catching it, Philip.

var notestxt = "Lorem ipsum dolor sit amet 09999 #08220 #082201, 082202 consectetuer adipiscing elit. Ut eleifend ipsum nec risus. Proin arcu ligula, part# 10046512 hendrerit et, fringilla a, dapibus sed, libero."


Code:
var A=[], M;
var Rx=/[\s#](\d{5,6})(?!\d)/g;
while((M=Rx.exec(notestxt))!=null){
    A[A.length]=M[1];
}
alert(A) // returns [09999,08220,082201,082202]
Once again, shows that there are more ways than one to kill a cat.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Tags
regex, regular expression

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:36 AM.


Advertisement
Log in to turn off these ads.