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 5 of 5

Thread: Regex issue

  1. #1
    New Coder
    Join Date
    Apr 2009
    Posts
    34
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Regex issue

    I am trying to grab all text between <START> and <END> and have the following bits of code, but neither are returning anything. I use JS so rarely that I can't see where the problems are -

    Code:
    var ermtext = response.match(/<START>(.*?)<END>/i);
        if (ermtext) {
            result.ermtext = ermtext.replace("$1");
        }
    Code:
    var patt=/<START>(.*?)<END>/i;
    result.ermtext = response.match(patt, "$1");

  • #2
    Banned
    Join Date
    Nov 2008
    Location
    not found
    Posts
    284
    Thanks
    0
    Thanked 53 Times in 51 Posts
    Code:
    var ermtext = response.match(/<START>(.*?)<END>/i);
        if (ermtext) {
            result.ermtext = ermtext[ 1 ];
        }

  • Users who have thanked randomuser773 for this post:

    blinks (06-23-2010)

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    As far as I know, match can give you matches but won't give you back the parenthesis capture, plus match only takes one argument, so the second one is flat out wrong.

    Try:
    Code:
    var patt=/<START>(.*?)<END>/i;
    document.write(patt.exec(response))
    The zeroth element will be the whole match. The first element will be the first parenthesis capture.

    EDIT: Oh, I'll just assume randomuser is right, and match does parenthesis capture... so you can go with either method

  • Users who have thanked Keleth for this post:

    blinks (06-23-2010)

  • #4
    New Coder
    Join Date
    Apr 2009
    Posts
    34
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Thanks randomuser and Keleth; you've helped me get further along, but I seem to have struck another problem. Looks like this time the problem is with the regex, specifically the (.*?) part.

    Looks like the dot does not encompass newline character, and I'm guessing that's the issue. But ([.\s]*?) doesn't work?

    TIA

  • #5
    New Coder
    Join Date
    Apr 2009
    Posts
    34
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Got it - changed the regex to ((.|\n)*)

    Thanks


  •  

    Posting Permissions

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