简体   繁体   中英

Regex - negative Negative Lookbehind with br

i have a string:

var a = '<<<<<<<br>>>>>>>>'

I want to replace: < with &lt; and > with &gt; , but I don't want to replace <> if it is the <br> tag. So, I'm doing this

a.replace(/<(?!br>)/g, '&lt;').replace(/>(?<!<br)/g, '&gt;')

first replace is working, but the second is not.

Javascript doesn't support lookbehinds so your expression is not valid.

For opening braces:

<(?!\/?\w)

For closing braces:

\B>

\\B denotes a non-word boundary.

JS:

> a.replace(/<(?!\/?\w)/g, '&lt;').replace(/\B>/g, '&gt;')
< "&lt;&lt;&lt;&lt;&lt;&lt;<br>&gt;&gt;&gt;&gt;&gt;&gt;&gt;"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM