javascriptregex

Javascript and regex: split string and keep the separator


I have a string:

var string = "aaaaaa<br />&dagger; bbbb<br />&Dagger; cccc"

And I would like to split this string with the delimiter <br /> followed by a special character.

To do that, I am using this:

string.split(/<br \/>&#?[a-zA-Z0-9]+;/g);

I am getting what I need, except that I am losing the delimiter. Here is the example: http://jsfiddle.net/JwrZ6/1/

How can I keep the delimiter?


Solution

  • I was having similar but slight different problem. Anyway, here are examples of three different scenarios for where to keep the deliminator.

    "1、2、3".split("、") == ["1", "2", "3"]
    "1、2、3".split(/(、)/) == ["1", "、", "2", "、", "3"]
    "1、2、3".split(/(?=、)/) == ["1", "、2", "、3"]
    "1、2、3".split(/(?!、)/) == ["1、", "2、", "3"]
    "1、2、3".split(/(.*?、)/) == ["", "1、", "", "2、", "3"]
    

    This works because as mentioned on MDN:

    If separator is a regex, the returned Array also contains the captured groups for each separator match

    Warning: The fourth will only work to split single characters. ConnorsFan presents an alternative:

    // Split a path, but keep the slashes that follow directories
    var str = 'Animation/rawr/javascript.js';
    var tokens = str.match(/[^\/]+\/?|\//g);