如何使用Jquery表达式用特定索引中的超链接替换单词?
我使用下面的函数用链接替换“#”标签,
$('.content').each(function(index) {
$(this).html($(this).html().replace(/(#\w+)/g, "<a target='_self' class='msg_links' href='http://test.com/search/q=$1'>$1</a>"));
});
假设字符串是这样的:
"Hello how are you #frineds, whats going on?"
它像这样重新运行:
"Hello how are you <a href='http://test.com/search/q=#frineds'>#frineds</a>, whats going on?"
而不是我想要这样
"Hello how are you <a href='http://test.com/search/q=%23frineds'>#frineds</a>, whats going on? "
我该怎么做使用Jquery?
-谢谢 阿布舍克
I replace "#" tag with link by using below function,
$('.content').each(function(index) {
$(this).html($(this).html().replace(/(#\w+)/g, "<a target='_self' class='msg_links' href='http://test.com/search/q=$1'>$1</a>"));
});
Suppose string is like this :
"Hello how are you #frineds, whats going on?"
It retruns like this :
"Hello how are you <a href='http://test.com/search/q=#frineds'>#frineds</a>, whats going on?"
Instead of I want to like this
"Hello how are you <a href='http://test.com/search/q=%23frineds'>#frineds</a>, whats going on? "
How I can do this using Jquery?
-Thanks
Abhishek
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在匹配的字符串上使用
encodeURIComponent
:或者从匹配中排除
#
并使%23
硬编码:You either need to use
encodeURIComponent
on the matched string:Or exclude the
#
from the match and make the%23
hard coded: