pistacchio
pistacchio

Reputation: 58863

Regex split by word or tag

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

Upvotes: 2

Views: 243

Answers (3)

Anton Sivov
Anton Sivov

Reputation: 414

Looks like this

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

You will get a list of matches.

Edited:

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

Upvotes: 0

skunkfrukt
skunkfrukt

Reputation: 1570

This should do it:

myString.match(/<[^>]*>|[^\s<]+/g)

Upvotes: 1

burning_LEGION
burning_LEGION

Reputation: 13450

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

Upvotes: 5

Related Questions