Reputation: 4157
I need to break a string:
Examples:
Hello,_have a beer_or a sandwich.$Bye => Hello,
_have a beer
_or a sandwich.
*Bye
$Hello,_have a beer,$or a sandwich.*Bye => $Hello,
_have a beer,
$or a sandwich.
*Bye
I came up with this regex:
/_.*?(?=[_\*\$])|\*.*?(?=[_\*\$])|\$.*?(?=[_\*\$])/g
The problem is:
* _ $
. (In the first example Hello
is not included.)* _ $
. (in both examples *Bye
is not included)I've tried adding ^.*?(?=[_\*\$])
for the first part, but then parts starting with * _ $
don't get selected.
Upvotes: 3
Views: 47
Reputation: 386560
You could use a positive lookeahead with the wanted chracters for splitting.
var string = '$Hello,_have a beer,$or a sandwich.*Bye_ ',
splitted = string.split(/(?=[$_*])/);
console.log(splitted);
Upvotes: 2