如何使用Jquery表达式将特定索引中的单词替换为超链接?
我使用下面的函数将“#”标记替换为link如何使用Jquery表达式将特定索引中的单词替换为超链接?,jquery,regex,javascript-framework,Jquery,Regex,Javascript Framework,我使用下面的函数将“#”标记替换为link $('.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 <a hre
$('.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 <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实现这一点
-谢谢
Abhishek您需要在匹配的字符串上使用
encodeURIComponent
:
$(this).html($(this).html().replace(/(#\w+)/g, function($0, $1) {
return "<a target='_self' class='msg_links' href='http://test.com/search/q=" + encodeURIComponent($1) + "'>" + $1 + "</a>");
});
您需要对匹配的字符串使用
encodeURIComponent
:
$(this).html($(this).html().replace(/(#\w+)/g, function($0, $1) {
return "<a target='_self' class='msg_links' href='http://test.com/search/q=" + encodeURIComponent($1) + "'>" + $1 + "</a>");
});
$(this).html($(this).html().replace(/#(\w+)/g, "<a target='_self' class='msg_links' href='http://test.com/search/q=%23$1'>$1</a>"));