squarefrog
squarefrog

Reputation: 4822

Regex: Matching a pattern but excluding part of the pattern

I'm struggling to figure out how to exclude my match from the following sample:

myJavascriptArray(["foo","bar"]);

I can match the string no problem with:

myJavascriptArray\(.+\);

Really, all I want is the ["foo","bar"] part.

I can ignore the myJavascriptArray part using:

(?!myJavascriptArray)\(.+\);  // matches (["foo","bar"]);

But then I'm completely lost!

Upvotes: 3

Views: 116

Answers (1)

anubhava
anubhava

Reputation: 784898

You can use:

string.match( /myJavascriptArray\((.+?)\)/ )[1];

Or to safeguard:

var m = string.match( /myJavascriptArray\((.+?)\)/ );
var myval = m ? m[1] : "";

This capturing the interested value in a group and use that group in output array.

Upvotes: 3

Related Questions