Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
665 views
in Technique[技术] by (71.8m points)

regex - Javascript split include delimiters

I have the following regular expression:

(</?[a-z][a-z0-9]*[^<>]*>)

I have the following text:

<DIV><P class='abc'>Hello <B>Mister</B>! How are you >..< doing? </P>
<I>I'm good</I></DIV>

Now I'd like to split the text per tag:

<DIV>
<P class='abc'>
Hello 
<B>
Mister
</B>
! How are you >..< doing?

</P>
<I>
I'm good
</I>
</DIV>

How can I do this with Javascript regex?
Is was able to get it to work but had to start over because javascript doesn't support lookbehinds.

(basically split on html tags and keep the delimiters)

Edit:
My goal with this is to use html to store formatting. I want to feed the html above to a javascript object. The javascript object separates the formatting from the text and does action A for formatting objects and action B for regular text.

I know it sounds a bit vague, but I don't want to reveal too much about the project.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I actually agree with Omar on this issue, but I'll give you the regex anyway. :)

<[^>]+?>|.+?(?=(?:<[^><]+?>|$))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...