Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

regex / javascript question

Options
  • 25-10-2007 12:21am
    #1
    Registered Users Posts: 21,264 ✭✭✭✭


    Greasemonkey again. :)

    I've pulled in a DIV InnerHTML to a var.

    Now I want to find occurrences of a string surrounded by tokens.

    Example:

    part number: 00000 - description -

    So the same part for every reference of it would be the bits in red.

    So anyone know the regex to do this? Also do I need to do a function for this? As I tried match('part number^') as a test but that only returned 1 instance which wasn't in an array.


Comments

  • Subscribers Posts: 4,076 ✭✭✭IRLConor


    Hobbes wrote: »
    Greasemonkey again. :)

    I've pulled in a DIV InnerHTML to a var.

    Now I want to find occurrences of a string surrounded by tokens.

    Example:

    part number: 00000 - description -

    So the same part for every reference of it would be the bits in red.

    So anyone know the regex to do this? Also do I need to do a function for this? As I tried match('part number^') as a test but that only returned 1 instance which wasn't in an array.

    So with that string above you want to extract the "00000 - description" portion?

    If so, I've tested this and it works:
    var testString = "part number: 00000 - description -";
    var regex = /part number:\s*([^-]+-[^-]+)-/;
    var result = regex.exec(testString);
    if (result != null) {
        alert("Captured \"" + result[1] + "\"");
    } else {
        alert("Did not match!");
    }
    


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Well I'd settle with just the number tbh.

    I tried that and it kind of worked for the first, but second item in the array was the same string?


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Have a look at regulazy .. dead handy for helping you with regular expressions


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Cheers! Solved it straight up.. that is going to remove so much heart ache from me. :D

    For those interested.
    http://tools.osherove.com/Default.aspx?tabid=182


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    I found that the plugin Firebug is way better for doing regex coding on the fly.


  • Advertisement
  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Firebug's the bomb.


Advertisement