向复杂的正则表达式添加例外(使用前瞻和后视)

Add exceptions to complex regular expression (lookahead and lookbehind utilized)

我需要一些有关正则表达式的帮助,因为我不是很熟悉。 到目前为止,我已经创建了以下正则表达式:

/\b(?<![\#\-\/\>])literal(?![\<\'\"])\b/i

https://regex101.com/ 所述:

\b assert position at a word boundary (^\w|\w$|\W\w|\w\W)

Negative Lookbehind (?])

Assert that the Regex below does not match

Match a single character present in the list below [#-/>]

# matches the character # literally (case insensitive)

- matches the character - literally (case insensitive)

/ matches the character / literally (case insensitive)

> matches the character > literally (case insensitive)

literal matches the characters literal literally (case insensitive)

Negative Lookahead (?![\<\'\"])

Assert that the Regex below does not match

Match a single character present in the list below [\<\'\"]

\< matches the character < literally (case insensitive)

\' matches the character ' literally (case insensitive)

\" matches the character " literally (case insensitive)

\b assert position at a word boundary (^\w|\w$|\W\w|\w\W)

Global pattern flags

i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])

我想为这个匹配规则添加两个例外。 1) 如果“>”前面有 "p",例如 <p> 起始标记,仅匹配文字。 2) 此外,仅当 < 后跟 /p 时才应匹配文字,例如 </p> 结束标记。 怎样才能做到这一点?

示例:只有粗体应该匹配。

<p>
    **Literal** in computer science is a
    <a href='http://www.google.com/something/literal#literal'>literal</a>
    for representing a fixed value in source code. Almost all programming 
    <a href='http://www.google.com/something/else-literal#literal'>languages</a>
    have notations for atomic values such as integers, floating-point 
    numbers, and strings, and usually for booleans and characters; some
    also have notations for elements of enumerated types and compound
    values such as arrays, records, and objects. An anonymous function
    is a **literal** for the function type which is **LITERAL**
</p>

我知道我把事情想得太复杂了,但情况本身就很复杂,我想我没有别的办法。

如果您要搜索的文本只是混合了一些 <a> 标签的文本,那么您可以简化环视的 <> 部分,并给出具体的不应后跟的字符串:</a>.

/\b(?<![-#\/])literal(?!<\/a>)\b/i

Regex101 Demo