I am trying to match pieces of JS code and retrieve string literals that contain the given keyword using Java.
After I tried to create my own regular expression, I decided to change this generalized string literal corresponding to regexp (Pattern.COMMENTS used when building patterns in Java):
(["'])
(?: \\? +.) *?
\1
next
(["'])
(?: \\? +.) *?
keyword
(?: \\? +.) *?
\1
Testing:
var v1 = "test";
var v2 = "testkeyword";
var v3 = "test"; var v4 = "testkeyword";
The regular expression does not match line 1 correctly and matches line 2 correctly.
However, on line 3, instead of just matching "testkeyword", it matches the snippet
"test"; var v4 = "testkeyword"
- , .
- , ?
PS: , Regexp ( ).