我的要求是这样的字符串,
Edit the Expression &1 Text to & se&e matches ). Roll & over ma&tches & or t
我需要选择所有"&"字符,忽略编码中的字符。我已经实现了选择所有编码字符。这是一个演示。现在我需要忽略它们,选择其他"&"。
您的正则表达式可能被视为正在进行的工作,例如,为了匹配&
您也可以将当前的正则表达式编写为&(?:#x?)?(?:d{2}|w{4});
。为了概括一下,您甚至可以将其更改为/&(?:#x?)?w{1,4};/
.
您的问题是如何否定这些实体,并在所有其他位置匹配&
。通过捕获组和一些代码很容易实现。
var s = "Edit the Expression & Text to & see matches ). Roll & over ma&tches & or the expression for details. Undo mistakes with ctrl-z. Save Favorites & Share expressions with friends or the Community. Explore & your results with & Tools. A full & Reference & Help is ava&ilable in & the Library, or watch the video Tutorial. & or & or &";
var re = /(&(?:#x?)?w{1,4};)|&/g;
var result = s.replace(re, function($0,$1) {return $1 ? $1 : "&";});
console.log(result);
在这里,模式是/(&(?:#x?)?w{1,4};)|&/g
-(<YOUR_NEGATED_PATTERN>)|&
.您的模式被捕获到组 1 中,当找到匹配项时,将检查组 1 值:如果组 1 匹配,则实体将放回生成的字符串中。所有其他&
都变成了&
。