I have written a regular expression to match some tags that look like this:
@("hello, world" bold italic font-size="15")
I want the regular expression to match these strings: ['hello, world', 'bold', 'italic', 'font-size="15"']
.
However, only these strings are matched: ['hello, world', 'font-size="15"']
.
Other examples:
- (success)
@("test") -> ["test"]
- (success)
@("test" bold) -> ["test", "bold"]
- (fail)
@("test" bold size="15") -> ["test", "bold", 'size="15"']
I have tried using this regular expression:
\@\(\s*"((?:[^"\\]|\\.)*)"(?:\s+([A-Za-z0-9-_]+(?:\="(?:[^"\\]|\\.)*")?)*)\s*\)
A broken down version:
\@\(
\s*
"((?:[^"\\]|\\.)*)"
(?:
\s+
(
[A-Za-z0-9-_]+
(?:
\=
"(?:[^"\\]|\\.)*"
)?
)
)*
\s*
\)
The regular expression is trying to
- match beginning of the sequence (
$(
), - match a string with escaped characters,
- match some (>= 1) blanks,
- (optional, grouped with (5)) match a
=
sign, - (optional, grouped with (4)) match a string with escaped characters,
- repeat (3) - (5)
- match end of the sequence (
)
)
However, this regular expression only matches "hello, world"
and font-size="15"
. How can I make it also match bold
and italic
, i.e. to match the group ([A-Za-z0-9-_]+(?:\="(?:[^"\\]|\\.)*")?)
multiple times?
Expected result: ['"hello, world"', 'bold', 'italic', 'font-size="15']
P.S. using JavaScript native regular expression