1

我不能让这个正则表达式在 javascript 中工作:

pat = new RegExp("Lorem(?=([^<]*($|<a|<[^/]|</[^a]))*($|(?<=a)))", "g");
test = test.replace(pat, '<a href="#">Lorem</a>');

我得到错误:invalid quantifier

但这里工作:http ://gskinner.com/RegExr/?2va0g

谢谢你的帮助 :)

编辑

我需要用链接替换每次出现的“字符串”,但前提是“字符串”还不是链接。

埃斯。

This is a <a href="#">String</a>. This is another String.

=>

This is a <a href="#">String</a>. This is another <a href="#">String</a>.

如果还可以检查链接 es 的 url,那就更好了:

This is a <a href="http://www.test.com/String">link</a>

=>

This is a <a href="http://www.test.com/String">link</a>

可能足以替换所有不在内部的 String <a ... </a>(第一个 TAG 不是故意关闭的)

4

1 回答 1

0

在此处查看此示例

var toreplace = "String"; // Text to find
var rx = new RegExp ("((?!\/)" + toreplace + "(?!\"))(?!<(.*?)>)", "g");
var txt = "This is a <a href=\"#\">String</a>. This is another String."; // Text to replace.
var newtxt = txt.replace (rx, '<a href="#">$1</a>'); // New Text

结果是:

This is a <a href="#">String</a>. This is another String.

=>

This is a <a href="#">String</a>. This is another <a href="#">String</a>.
于 2012-10-30T18:37:48.950 回答