Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 匹配除尖括号外的单词边界_Javascript_Html_Xml_Regex_Frontend - Fatal编程技术网

Javascript 匹配除尖括号外的单词边界

Javascript 匹配除尖括号外的单词边界,javascript,html,xml,regex,frontend,Javascript,Html,Xml,Regex,Frontend,我试图在一大块文本中进行多次替换,并将单词转换为带有HTML标记的超链接。我发现使用表达式\bword\b在大多数情况下都能找到我想要的单词,但一个问题是尖括号显然算作边界,因此当我在同一个字符串上再次运行表达式时,我会匹配已转换为链接的单词。我刚刚在表达式[\s-\u]word[\s-\u]中找到了一个解决方法,但这需要我知道单词周围允许哪些字符,而不是禁止使用字符。那么,有没有一种方法可以让我有一个表达,说‘将这个词与边界匹配,除了 注意-我不能使用全局标志。这意味着用于在文本块中进行“n”

我试图在一大块文本中进行多次替换,并将单词转换为带有HTML标记的超链接。我发现使用表达式\bword\b在大多数情况下都能找到我想要的单词,但一个问题是尖括号显然算作边界,因此当我在同一个字符串上再次运行表达式时,我会匹配已转换为链接的单词。我刚刚在表达式[\s-\u]word[\s-\u]中找到了一个解决方法,但这需要我知道单词周围允许哪些字符,而不是禁止使用字符。那么,有没有一种方法可以让我有一个表达,说‘将这个词与边界匹配,除了

注意-我不能使用全局标志。这意味着用于在文本块中进行“n”替换,介于1和all之间


你可以尝试一个消极的回顾,比如:


?使用JavaScript遍历DOM,并仅将正则表达式替换应用于文本节点。文本节点?我不知道你的意思。DOM中任何不是标记的部分。查看我链接的代码片段。如果您用替换代码替换第三行,它应该可以正常工作。看起来它可以工作,但我担心如果您处理大量文本,例如新闻文章或其他内容,它可能会占用大量资源。这是唯一可靠的解决方案。您需要区分标记内部和标记外部的引用。这实际上相当于解析HTML。和
var str = "Plain planes are plain.  Plain pork is plain.  Plain pasta is plainly plain.";
str = str.replace(/(\b)(plain)(\b)/, "$1<a href='www.plain.com'>$2</a>$3");
// this will result in the first instance of 'plain' becoming 
// a link to www.plain.com

str = str.replace(/(\b)(plain)(\b)/, "$1<a href='www.plain.com'>$2</a>$3");
// this will NOT make the second instance of 'plain' into 
// a link to www.plain.com
// instead, the 'plain' in the middle of the anchor tag 
// is matched and replaced causing nested anchor tags