javascript - Match simple regex pattern using JS (key: value) -
i have simple scenario want match follow , capture value:
stuff_in_string, env: 'local', // want match , capture content in quotes more_stuff_in_string
i have never written regex pattern before excuse attempt, aware totally wrong.
this trying say:
- match "env:"
- followed none or more spaces
- followed single or double quote
- capture until..
- the next single or double quote
/env:*?\s+('|")+(.*?)+('|")/g
thanks
ps here #failed fiddle: http://jsfiddle.net/dfhge/
note: regex ended using (not answer below overkill needs): /env:\s+(?:"|')(\w+)(?:"|')/
you can use this:
/\benv: (["'])([^"']*)\1/g
where \1
backreference first capturing group, content in second. simple way simple cases.
now, other cases like:
env: "abc\"def" env: "abc\\" env: "abc\\\def" env: "abc'def"
you must use more constraining pattern:
first: avoid different quotes problem:
/\benv: (["'])((?:[^"']+|(?!\1)["'])*)\1/g
i put possible content in non capturing group can repeat @ will, , use negative lookahead (?!\1)
check if allowed quote not same captured quote.
second: backslash problem:
if quote escaped, can't closing quote! must check if quote escaped or not , allow escaped quotes in string.
i remove backslashes allowed content:
/\benv: (["'])((?:[^"'\\]+|(?!\1)["'])*)\1/g
i allow escaped characters:
/\benv: (["'])((?:[^"'\\]+|(?!\1)["']|\\[\s\s])*)\1/g
to allow variable number of spaces before quoted part, can replace :
:\s*
/\benv:\s*(["'])((?:[^"'\\]+|(?!\1)["']|\\[\s\s])*)\1/g
you have working pattern.
third: pattern optimization
a simple alternation:
using capture group , backreferences can seducing deal different type of quotes since allow write pattern in concise way. however, way needs create capture group , test lookahead in part (?!\1)
["']`, not efficient. writing simple alternation increases pattern length , needs use 2 captures groups 2 cases more efficient:
/\benv:\s*(?:"((?:[^"\\]+|\\[\s\s])*)"|'((?:[^'\\]+|\\[\s\s])*)')/g
(note: if decided must check 1 of 2 capture groups defined.)
unrolling loop:
to match content inside quotes use (?:[^"\\]+|\\[\s\s])*
(for double quotes here) works can improved reduce amount of steps needed. unroll loop consists avoid alternation:
[^"\\]*(?:\\[\s\s][^"\\]*)*
finally whole pattern can written this:
/\benv:\s*(?:"([^"\\]*(?:\\[\s\s][^"\\]*)*)"|'([^'\\]*(?:\\[\s\s][^'\\]*)*)')/g
Comments
Post a Comment