包装文本不是标签的一部分

时间:2022-11-29 12:05:42

I'm trying to highlight (with <mark) a search string inside another string, but it shouldn't be part of a tag (i.e. te in <cite> shouldn't match, but te in test should). I've been using code like this to replace the match with the second regex listed, but that only works nestted.

我试图突出显示(使用 中的te不应该匹配,但是test中的te应该匹配)。我一直在使用这样的代码来替换列出的第二个regex,但这只适用于nestted。 )另一个字符串中的搜索字符串,但它不应该是标记的一部分(例如,

someElement.innerHTML.replace(regex, function(full, before, match, after) {
  return before + '<mark>' + match + '</mark>' + after;
});

Suggestions I tried:

建议我尝试:

  • <\/\w*>(\w*\s*)*(te)(\w*\s*)*<([a-z]*\s*\w*="\w*")*> (doesn't match at all?)
  • < \ / \ w * >(\ w * \ s *)*(te)(\ w * \ s *)* <([a - z]* \ s * \ w * = \ w *)* >(不匹配?)
  • (<.+?>[^<>]*?.*)(te)(.*[^<>]*?<.+?>) (only works on the nested tags)
  • (< + ? >[^ < >]* ?。*)(te)(。*[^ < >]* ? < + ? >)(只适用在嵌套标签)
  • ((<.+?>[^<>]*?)?.*)(te)(.*([^<>]*?<.+?>)?) (.replace gives undefined's)
  • (< + ? >[^ < >]* ?)?。*)(te)(。*((^ < >]* ? < + ? >)吗?)(。取代了未定义的)

Example:

例子:

String to wrap: te

字符串包装:te

Input text:

输入文本:

‘This is a test string’
<cite> — some test wrapped too</cite>

Expected output:

预期的输出:

‘This is a <mark>te</mark>st string’
<cite> — some <mark>te</mark>st wrapped too</cite>

I've browsed a lot of "duplicates", but couldn't find an exact one, but if someone can lead me to a working example, that'd be lovely too, thanks!

我浏览了很多“复制品”,但是找不到一个确切的复制品,但是如果有人能给我一个工作的例子,那也太好了,谢谢!

An answer that's now deleted which worked the best for me is (te)(?![^<]*>) (by revo). This does however not work if the next char is >, but I'll see that as a unsolved edge case.

答案现在删除它的最好的工作了我(te)(? ![^ <]* >))(通过。但是,如果下一个char是>,那么这将不起作用,但是我将把它视为一个未解决的边缘情况。

1 个解决方案

#1


3  

Regex:

正则表达式:

(te)(?!\w*>)

It benefits from a negative lookahead to check if it is within an opening / closing tag or not. (?!\w*>) this will ignore any te that is followed by any word characters and > (which possibly denotes being within a tag name.)

它可以从消极的前视中获益,检查它是否在开始/结束标记中。(?!\w*>)这将忽略后面跟着任何单词字符和>的任何te(可能表示在标记名内)。

Failing case:

When there is potentially a string like test> (unpaired) within input string even though it is not a tag it doesn't match.

当输入字符串中可能有一个字符串,比如test>(未配对),即使它不是一个标记,它也不匹配。

Live demo

现场演示

#1


3  

Regex:

正则表达式:

(te)(?!\w*>)

It benefits from a negative lookahead to check if it is within an opening / closing tag or not. (?!\w*>) this will ignore any te that is followed by any word characters and > (which possibly denotes being within a tag name.)

它可以从消极的前视中获益,检查它是否在开始/结束标记中。(?!\w*>)这将忽略后面跟着任何单词字符和>的任何te(可能表示在标记名内)。

Failing case:

When there is potentially a string like test> (unpaired) within input string even though it is not a tag it doesn't match.

当输入字符串中可能有一个字符串,比如test>(未配对),即使它不是一个标记,它也不匹配。

Live demo

现场演示