[英]What should be the regex to match < except for mark tag?
Basically I need to match any < from a text that isn't part of a <mark>
or </mark>
tag.基本上,我需要匹配不属于
<mark>
或</mark>
标签的文本中的任何< 。 This is what I got so far, but it doesn't really work as expected and I've been trying for some time now.这是我到目前为止所得到的,但它并没有真正按预期工作,我已经尝试了一段时间。
/(<)(?=[^mark]|$)/g
I need the regex to work in Javascript.我需要正则表达式在 Javascript 中工作。
I would like it to match every < except for the ones come in the mark tags.我希望它匹配每个 < 除了标记标签中的那些。
like the < which are in ** should match in the below example.像 ** 中的 < 应该在下面的例子中匹配。
As an example:举个例子:
hello, <mark id="abc-Def">how</mark> are you?
where **<**span>are**<**/span> you <mark>from</mark>?
**<**noice id="jk">product**<**/noice>
Use用
/<(?!\/?mark\b[^>]*>)/gi
Replace with **$&**
.替换为
**$&**
。 See proof .见证明。
Explanation解释
--------------------------------------------------------------------------------
< '<'
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
\/? '/' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
mark 'mark'
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
[^>]* any character except: '>' (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
> '>'
--------------------------------------------------------------------------------
) end of look-ahead
JavaScript: JavaScript:
const text = `hello, <mark id="abc-Def">how</mark> are you? where <span>are</span> you <mark>from</mark>? <noice id="jk">product</noice>`; console.log(text.replace(/<(?!\\/?mark\\b[^>]*>)/gi, `**$&**`));
Have you tried this?你试过这个吗? /<(?!(mark|/mark))/
/<(?!(mark|/mark))/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.