正则表达式只有空格的Word边界 [英] Regex Word boundary for only whitespace

查看:71
本文介绍了正则表达式只有空格的Word边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的新RegExp((\\\\+ var +\\ b))的正则表达式有问题。我遇到的问题是,如果我的文本中有一个span元素并且我正在替换 word span,它会将span元素替换为。我认识到<的问题计为单词边界,因此它找到< ** span **> 但我不想要它。那么在正则表达式中是否有任何单词边界仅用于空格并且行为就像 \b

I'm having trouble with my regex of new RegExp("(\\b" + var + "\\b)"). The problem I'm having is that if there is a span element in my text and I'm replacing the word span, it replaces the span element to. I recognized the problem of "<" counting as a 'word boundary' so it finds <**span**> yet I do not want it to. So is there any word boundary in regex that is only for whitespace and behaves just like \b?

推荐答案

唯一安全的方法是迭代文本节点并在其中进行替换。以下是使用 TreeWalker 的解决方案:

The only safe way to do this is to iterate over the text nodes and do the replacement within them. Here is one solution using TreeWalker:

var regexp = new RegExp("\\b" + src_text + "\\b", "g");
var walker = document.createTreeWalker(elt, NodeFilter.SHOW_TEXT);
var node = walker.nextNode();

while (node) {
  let nextNode = walker.nextNode();
  let newNode = document.createElement('span');
  newNode.innerHTML = node.textContent.replace(regexp, "foo");
  node.parentNode.replaceChild(newNode, node);
  node = nextNode;
}

您可能还想转义 src_text中的字符具有特殊的正则表达式含义。您可以通过快速搜索轻松找到如何做到这一点。例如,请参阅此问题

You will probably also want to escape characters within src_text which have special regexp meanings. You can find out how to do that easily enough with a quick search. For example, see this question.

在您的情况下,替换的字符串包含HTML。这将需要更多的工作。我们创建一个新节点并设置其innerHTML,然后用新的节点替换旧节点。

In your case, the replaced string contains HTML. That will require a bit more work. We create a new node and set its innerHTML, then replace the old node with the new one.

小提琴: https://jsfiddle.net/zanhaz0j/

这篇关于正则表达式只有空格的Word边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆