Reputation: 66590
I have as string that I'm splitting into arguments using this regex:
/('[^']*'|"(\\"|[^"])*"|(?:\/(\\\/|[^\/])+\/[gimy]*)(:? |$)|(\\ |[^ ])+|[\w-]+)/gi;
it work fine for string like this:
'test "foo bar" baz /^asd [x]/ str\\ str 10 1e10';
exept regular expressions have space at the end because of (:? |$)
I've fixed that by removing space after split, but is there a way to do this with just regex modification?
Upvotes: 1
Views: 243
Reputation: 149040
You could change it to a lookahead ((?=…)
):
/('[^']*'|"(\\"|[^"])*"|(?:\/(\\\/|[^\/])+\/[gimy]*)(?= |$)|(\\ |[^ ])+|[\w-]+)/gi;
I'd also consider changing the spaces to \s
(and [^ ]
to \S
) in order to handle other whitespace characters like tabs and line breaks.
Upvotes: 1
Reputation: 10838
Change (:? |$)
to (?=:? |$)
(?=
is a positive lookahead, so it will enforce matching, but not be returned)
Upvotes: 1