我想在 div 中的链接周围创建一个强大的标签。这些链接动态显示。
我尝试这样做:
$('#alertWrapp').each(function() {
$(this).html($(this).text()
.replace(/#[a-z0-1A-Z]+/g, '<span style="color: #ed6567;">$&</span>')
.replace('http://', '<strong>$&</strong>')
);
});
但是我无法继续正则表达式,它太复杂了......所以有可能构建一个正则表达式来找到http://协议(protocol)空间?因为它是一个空格,设置了我的链接的结尾...
谢谢你
最佳答案
要替换 http://[anything-until-space] 或 https://[anything] 试试这个
$('#alertWrapp').each(function() {
$(this).html(
$(this).text()
.replace(/#[a-z0-1A-Z]+/g, '<span style="color: #ed6567;">$&</span>')
.replace(/https?:\/\/[^ ]+/g, '<strong>$&</strong>')
);
});
关于javascript - 使用replace()将链接包装在字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22363685/