Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
662 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 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...