我想找到文本中的每个 url 并用标签 ( <a href="...">...</a>
) 包裹它们。
var src = "bla blaaa blaaaaaa 1 http://yahoo.com text something about and. http://www.google.com";
var match = /http:\/\/([a-z0-9.-]+)/.exec(src); //this only can one matched
// result: ["http://yahoo.com", "yahoo.com"]
但我需要包装每个链接。
最佳答案
您可以使用 /g
(全局)来匹配所有出现和反向引用,如下所示:
var src = "bla blaaa blaaaaaa 1 http://yahoo.com text something about and. http://www.google.com";
var match = src.replace(/http:\/\/([a-z0-9.-]+)/g, '<a href="$1">$1</a>');
关于javascript - javascript 中的正则表达式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4077059/