Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Sep 2007
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,346
    Thanks
    11
    Thanked 589 Times in 570 Posts
    would
    matches = notestxt.match(/[\s\#]\d{5,6}?/g);

    work?
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #3
    New Coder
    Join Date
    Sep 2007
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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)

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,346
    Thanks
    11
    Thanked 589 Times in 570 Posts
    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 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • Users who have thanked rnd me for this post:

    rhyno (10-15-2008)

  • #5
    New Coder
    Join Date
    Sep 2007
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    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.

  • #7
    New Coder
    Join Date
    Sep 2007
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,346
    Thanks
    11
    Thanked 589 Times in 570 Posts
    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 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,011
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.

  • Users who have thanked Philip M for this post:

    mrhoo (10-16-2008)

  • #10
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    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.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,011
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •