我试一试:
var str = "some description +first tag, +second tag\n" +
"+third tag +fourth tag\n" +
"+tag from new line\n" +
"+tag1+tag2+tag3";
var tags = str.match(/\+[^+,\n\s].+?(?=\s*[\+,\n]|$)/g);
这导致tags
:
[ '+first tag',
'+second tag',
'+third tag',
'+fourth tag',
'+tag from new line',
'+tag1',
'+tag2',
'+tag3' ]
详细说明:
\+ // Starts with a '+'.
[^+,\n\s] // Doesn't end immedatedly (empty tag).
.+? // Non-greedily match everything.
(?= // Forward lookahead (not returned in match).
\s* // Eat any trailing whitespace.
[\+,\n]|$ // Find tag-ending characters, or the end of the string.
)