1

I'm trying to write a regex for use in javascript.

var script = "function onclick() {loadArea('areaog_og_group_og_consumedservice', '\x26roleOrd\x3d1');}";
var match = new RegExp("'[^']*(\\.[^']*)*'").exec(script);

I would like split to contain two elements:

match[0] == "'areaog_og_group_og_consumedservice'";

match[1] == "'\x26roleOrd\x3d1'";

This regex matches correctly when testing it at gskinner.com/RegExr/ but it does not work in my Javascript. This issue can be replicated by testing ir here http://www.regextester.com/.

I need the solution to work with Internet Explorer 6 and above.

Can any regex guru's help?

2
  • Can you describe the behavior you are experiencing? Does it match at all? or just match the wrong section? Commented Jan 29, 2011 at 2:25
  • I'm getting the first match but second one appeared to be empty. Commented Jan 29, 2011 at 13:15

3 Answers 3

1

Judging by your regex, it looks like you're trying to match a single-quoted string that may contain escaped quotes. The correct form of that regex is:

'[^'\\]*(?:\\.[^'\\]*)*'

(If you don't need to allow for escaped quotes, /'[^']*'/ is all you need.) You also have to set the g flag if you want to get both strings. Here's the regex in its regex-literal form:

/'[^'\\]*(?:\\.[^'\\]*)*'/g

If you use the RegExp constructor instead of a regex literal, you have to double-escape the backslashes: once for the string literal and once for the regex. You also have to pass the flags (g, i, m) as a separate parameter:

var rgx = new RegExp("'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'", "g");
while (result = rgx.exec(script))
    print(result[0]);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply, I seem to get the first match but the second one is undefined, I tried this at regextester.com matches: 0: ('areaog_og_group_og_consumedservice') 1: (undefined)
That 1: refers to the contents of the first capturing group; it came out as undefined because that part of the regex didn't participate in the match. As a matter of fact, there shouldn't even be a first capturing group; I meant to use a non-capturing group like I did in the other regexes. (I'll fix that.) If you want to match two or more strings, you have to call exec() repeatedly as I demonstrated.
Arh, ok, I didn't realise that you could call exec multiple times. Thanks for your help.
0

The regex you're looking for is .*?('[^']*')\s*,\s*('[^']*'). The catch here is that, as usual, match[0] is the entire matched text (this is very normal) so it's not particularly useful to you. match[1] and match[2] are the two matches you're looking for.

var script = "function onclick() {loadArea('areaog_og_group_og_consumedservice', '\x26roleOrd\x3d1');}";
var parameters = /.*?('[^']*')\s*,\s*('[^']*')/.exec(script);
alert("you've done: loadArea("+parameters[1]+", "+parameters[2]+");");

The only issue I have with this is that it's somewhat inflexible. You might want to spend a little time to match function calls with 2 or 3 parameters?

EDIT In response to you're request, here is the regex to match 1,2,3,...,n parameters. If you notice, I used a non-capturing group (the (?: ) part) to find many instances of the comma followed by the second parameter.

/.*?('[^']*')(?:\s*,\s*('[^']*'))*/

1 Comment

Thanks for reply and the heads up on ignoring the first match. This meet's the questions requirement, thank you. I should of also specified that sometime there's only one parameter in the script. Are you able to tweek it to allow for that please?
0

Maybe this:

'([^']*)'\s*,\s*'([^']*)'

1 Comment

Thanks for your reply, this works thank you. I should of also specified that sometime there's only one parameter in the script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.