Javascript and regex: split string and keep the separator

后端 未结 7 2267
轮回少年
轮回少年 2020-11-22 05:25

I have a string:

var string = \"aaaaaa
† bbbb
‡ cccc\"

And I would like to split this string w

7条回答
  •  太阳男子
    2020-11-22 05:53

    Use (positive) lookahead so that the regular expression asserts that the special character exists, but does not actually match it:

    string.split(/
    (?=&#?[a-zA-Z0-9]+;)/g);

    See it in action:

    var string = "aaaaaa
    † bbbb
    ‡ cccc"; console.log(string.split(/
    (?=&#?[a-zA-Z0-9]+;)/g));

提交回复
热议问题