开发者

regex to insert html tags with out creating invalid html

开发者 https://www.devze.com 2023-02-19 10:14 出处:网络
I have some javascript that is looking at a string of text based on a users selection and wrapping the string in a <span> tag.What I\'m looking for a regular expression that would look at the st

I have some javascript that is looking at a string of text based on a users selection and wrapping the string in a <span> tag. What I'm looking for a regular expression that would look at the string of text and check for existing html tags in the string, and if they exist, break up the span so that it doesn't invalidate the html.

For example, lets say i have the following text


<p>Lorem ipsum dolor sit amet, <i>consectetur adipiscing elit</i>. 
Curabitur tortor risus, facilisis vitae bibendum sit amet, mattis non dui.</p>

And the user selects "amet, <i>consectetur". The string should end u开发者_如何学编程p as "<span>amet, </span><i><span>consectetur</span>" as opposed tp "<span>amet, <i>consectetur</span>"


HTML shouldn't be parsed with RegEx. See: RegEx match open tags except XHTML self-contained tags

0

精彩评论

暂无评论...
验证码 换一张
取 消