Regexp to match Javascript string literals with a specific keyword using Java

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 ( ).

+3
5

:

(?:
    "
    (?:\\"|[^"\r\n])*
    keyword
    (?:\\"|[^"\r\n])*
    "
|
    '
    (?:\\'|[^'\r\n])*
    keyword
    (?:\\'|[^'\r\n])*
    '
)
+3

(. , :), , :

(?:
    "
    (?:\\?+"|[^"])*
    keyword
    (?:\\?+"|[^"])*
    "
|
    '
    (?:\\?+'|[^'])*
    keyword
    (?:\\?+'|[^'])*
    '
)
+1

, , . .

0

Rhino - JS Java - String.

, , , , " ".

, , , , , ( ). :

    var v5 =  "test\x6b\u0065yword"

, - , Regex Powertoy, , Java.

0

:

string-literal ::= quote text quote

text ::= character text
       | character

character ::= non-quote
            | backslash quote

, .

, (.. ), , , , .

, , , . , .

, (, ). .

, , , . , , , , .

. , , , --- , .

0

Source: https://habr.com/ru/post/1712289/


All Articles