Javascript -- get only the variable part of a regex match

11,777

Solution 1

Javascript should return an array object on a regex match, where the zero index of the array is the whole string that was matched and the following indexes are the capture groups. In your case something like:

var myVar = regexp.exec(myString)[1];

should assign the value of the (.*?) capture group to myVar.

Solution 2

(Quotes from MDC)

Including parentheses in a regular expression pattern causes the corresponding submatch to be remembered. For example, /a(b)c/ matches the characters 'abc' and remembers 'b'.

Since .*? is the first (and only) remembered match, use $1 in your replacement string:

var foo = myString.replace(regexp, '$1');

Edit: As per your comment, you can also (perhaps with clearer intention) do this:

var foo = regexp.exec(myString)[1];
Share:
11,777

Related videos on Youtube

morgancodes
Author by

morgancodes

Programmer/artist working on a monster javascript project for a cable television company for the past two and a half years. Meanwhile, I build magical sound experiences for iOS including Thicket, Morton Subotnick's Pitch Painter, and this fun toy payed for by gum. I also like to make geometric sculptures from paper. Future plans include releasing my C++ audio patching engine (build on top of STK) as an open source project, creating the world's most mesmerizing musical video game, building my own programming language, and finding a way to pay for it all.

Updated on April 27, 2022

Comments

  • morgancodes
    morgancodes almost 2 years

    given:

    var regexp = new RegExp("<~~include(.*?)~~>", "g");
    

    What's the easist way in javascript to assign a variable to whatever's matched by .*?

    I can do this, but it's a little ugly:

    myString.match(regexp).replace("<~~include", "").replace("~~>", "");
    
  • morgancodes
    morgancodes over 13 years
    Thanks bears. Thing is, I don't actually want to replace the whole thing, I just want to find out what the value of .*? is.
  • Matt Ball
    Matt Ball over 13 years
    Nick just commented (and then deleted his comment) that the first version will also work. He's correct, but I guess the intention of the replace version is less clear than the intention of the exec version. In either case, you have to assign the value returned by replace/exec to another variable.
  • Matt Ball
    Matt Ball over 13 years
    Lookaround is not the answer. How is that supposed to help?
  • Skyler
    Skyler over 13 years
    [quote] Thanks bears. Thing is, I don't actually want to replace the whole thing, I just want to find out what the value of .*? is. – morgancodes 15 mins ago
  • Skyler
    Skyler over 13 years
    So they could write a regex that Edit matches <~~include(.*?)~~> and excludes the information they do not want.
  • 1.21 gigawatts
    1.21 gigawatts over 7 years
    Won't this throw an error if there is no match? It returns null if there's no match so attempting to access the array index of a null object would throw an error.