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?

0

2 Answers 2

2

Use simple regex pattern

[\w-]+="[^"]*"
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that dit it! Thanks a lot guys
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]);
}

3 Comments

Hmmm, that's too bad. However, the params can also have space characters in them. So a string like [box style="metro action" url="someurl.com/"] would be applicable as well...
I see no reason to use \b in this case. If I am wrong, show me example of input where it makes difference.
@Ωmega: Perhaps you know more about "this case" than I do. I'm working based on what was in the question. The OP felt the need to use a lookbehind assertion; I wanted to offer something more than "drop it".

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.