Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Jquery表达式将特定索引中的单词替换为超链接?_Jquery_Regex_Javascript Framework - Fatal编程技术网

如何使用Jquery表达式将特定索引中的单词替换为超链接?

如何使用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

我使用下面的函数将“#”标记替换为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 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>"));