gpt4 book ai didi

javascript - 更改受正则表达式影响的字符串中 URL 的颜色

转载 作者:行者123 更新时间:2023-11-29 19:53:21 24 4
gpt4 key购买 nike

我正在尝试更改与某些 regular expression 匹配的 URL 的颜色在字符串中。

这是我的代码:

$(document).ready(function(){

//Change the color of words containing the @ sing
$("#modal_0 section p").html(function(_, html){
return html.replace(/(\@\w+)/g, '<span class="change_color">$1</span>');
}); //Works fine

//Change the color of words containing URLs (www.domain.com, domain.com, etc.)
$("#modal_0 section p").html(function(_, html){
return html.replace(/^(?=www\.)?[A-Za-z0-9_-]+\.+[A-Za-z0-9.\/%&=\?_:;-]+$/, '<span class="change_color">$1</span>');
}); //Doesn't work
})
;

当我尝试捕捉包含 @ sing 的单词时,该函数工作正常。而且我没有发现正则表达式有任何问题。

这是 the fiddle .

最佳答案

删除那些 anchor ,添加一个捕获组,它将起作用:

/(?=www\.)?([A-Za-z0-9_-]+\.+[A-Za-z0-9.\/%&=\?_:;-]+)/

演示:http://jsfiddle.net/JssZm/1/

关于javascript - 更改受正则表达式影响的字符串中 URL 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16889487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com