Javascript 用replace()将Wrapp链接改为强链接
我想在div中的链接周围做一个强标记。这些链接会动态显示 我试着这样做:Javascript 用replace()将Wrapp链接改为强链接,javascript,regex,replace,Javascript,Regex,Replace,我想在div中的链接周围做一个强标记。这些链接会动态显示 我试着这样做: $('#alertWrapp').each(function() { $(this).html($(this).text() .replace(/#[a-z0-1A-Z]+/g, '<span style="color: #ed6567;">$&</span>') .replace('http://', '<strong>$&</strong&g
$('#alertWrapp').each(function() {
$(this).html($(this).text()
.replace(/#[a-z0-1A-Z]+/g, '<span style="color: #ed6567;">$&</span>')
.replace('http://', '<strong>$&</strong>')
);
});
$('#alertWrapp')。每个(函数(){
$(this.html($(this.text)()
.更换(/#[a-z0-1A-Z]+/g,“$&”)
.replace('http://','$&'))
);
});
但是我不能继续正则表达式,它太复杂了。。。因此,有可能构建一个regex,找到http://protocole-to-space?因为这是一个空间谁设置了我的链接结束
谢谢你你为什么不设计一下锚呢。检查这个 或 如果是要用粗体包装的url文本,请尝试以下操作或选中此选项
var text=data.replace(/https?:\/\/[^]+/g,$&)
要替换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>')
);
});
$('#alertWrapp')。每个(函数(){
$(this.html)(
$(this.text())
.更换(/#[a-z0-1A-Z]+/g,“$&”)
.replace(/https?:\/\/[^]+/g,$&)
);
});
为什么不应用css类呢?因为我无法控制返回给我的文本。。。事实上,文本不仅是url,也是简单的字符串和哈希标记。这就是为什么我需要用javascript替换http://to space。我想他有纯文本格式的链接,没有html标记
var text = data.replace(/https?:\/\/[^ ]+/g, '<strong>$&</strong>')
$('#alertWrapp').each(function() {
$(this).html(
$(this).text()
.replace(/#[a-z0-1A-Z]+/g, '<span style="color: #ed6567;">$&</span>')
.replace(/https?:\/\/[^ ]+/g, '<strong>$&</strong>')
);
});