2

I'm parsing a string like this with javascript:

[box style="a" width="b" height="c"]

So far, when I use http://gskinner.com/RegExr/ it works and it parses fine using this regex:

/(?<=\s).+?=".+?"/

However, when I do this in javascript it errors out:

Uncaught SyntaxError: Invalid regular expression: /(?<=\s).+?=".+?"/: Invalid group

This is part of the code:

if (scOpenTag instanceof Array) {
   var params = scOpenTag[0].match(/(?<=\s).+?=".+?"/);                    
   for (var i = 0; i < params.length; i++)
      console.log(params[i]);
}

Somebody know what I'm doing wrong?

4

2 回答 2

2

Use simple regex pattern

[\w-]+="[^"]*"
于 2012-10-27T16:45:04.080 回答
1

JavaScript doesn't support lookbehind assertions; neither (?<=...) nor (?<!...) will work.

However, it looks like your keys/property-names/attribute-names/whatever-those-are have the form \w+, so you can get some mileage out of the word-boundary assertion \b (which matches either (?<!\w)(?=\w) or (?<=\w)(?!\w)):

/\b\w+="[^"]+"/

Edited to add: For that matter, you can get your exact current functionality by using a capture-group, and using params[1] instead of params[0]:

if (scOpenTag instanceof Array) {
   var params = scOpenTag[0].match(/\s(.+?=".+?")/);
   for (var i = 1; i < params.length; i++)
      console.log(params[i]);
}
于 2012-10-27T16:38:43.477 回答