2

How can I use regular expressions in Javascript in order to split a text by words (utf8) or tags? For example, given the following:

Lorem ipsum dolor <b>sid</b> amet et <a href="asdasd">amet et</a> fugit

I'd like to have it splitted like this:

Lorem 
ipsum 
dolor 
<b>
sid
</b> 
amet 
et 
<a href="asdasd">
amet
et
</a>
fugit
4

3 回答 3

5

use this regex <.+?>|\S+(?=<)|\S+

于 2012-10-03T10:34:36.550 回答
1

This should do it:

myString.match(/<[^>]*>|[^\s<]+/g)
于 2012-10-03T10:36:54.897 回答
0

Looks like this

(?s)(?:<.+?>)|(?:\S+)

You will get a list of matches.

Edited:

(?s)(?:<.+?>)|(?:\S+(?=<))|(?:\S+)
于 2012-10-03T10:36:01.780 回答