我有一些简单的正则表达式,应该将@Username
替换为
<a href="http://google.com">@Username</a>
虽然结果很奇怪。
REGEX
var msg="@Mr.EasyBB";
msg.replace(/@(.+?)/g,
'<a href=\"http://'+window.location.host+'/u=$1\">$1</a>');
有人可以帮助解决这个小问题的结果吗
<a href="http://google.com">@U</a>sername
答案 0 :(得分:1)
.+?
是懒惰匹配 - 这将匹配尽可能少的字符。
试试这个。它将匹配尽可能多的非空白字符。
/@(\S+)/
答案 1 :(得分:1)
试试这个:
msg = msg.replace(/@(\S+)/g, '<a href=\"http://'+window.location.host+'/u=$1\">@$1</a>');