I would like to split a paragraph with javascript if inside of the paragraph I have <br /> followed by newline and by a digit.
Here is the example of the paragraph:
aaaaaa aaaaa aaaaa aaaaa aaaa <br />
1 bbbb bbbbb bbbbb bbbb bbb <br />
2 cccc ccccc ccccc cccc ccc <br />
* dddd ddddd ddddd dddd ddd <br />
And I would like to have an array with 3 elements inside:
1. aaaaaa aaaaa aaaaa aaaaa aaaa <br />
2. 1 bbbb bbbbb bbbbb bbbb bbb <br />
3. 2 cccc ccccc ccccc cccc ccc <br />
* dddd ddddd ddddd dddd ddd <br />
I have tried this:
<br \/>\d
But this not worked because of the newline I guess.
Thank you very much for your help.
Assuming the string is stored in a variable called
str, the following should work:I have used:
/m), to allow the $ to match the end of one line, and the ^ to match the start of the next.[\s\S], as detailed here.(?=\d+). It is not included in the match, though, so the digits are not removed from the string.EDIT: This will remove the
<br />s from all but the last element in the list. Since I cannot think of a better way right now, you could just re-add them using something like:Not ideal, but seems to work for me.