简体   繁体   English

正则表达式将文本替换为@中的文本

[英]Regex Replace text with @ in it

I have some simple regex that should replace @Username to 我有一些简单的正则表达式应该取代@Username

<a href="http://google.com">@Username</a>

Though its getting very odd results. 虽然它的结果很奇怪。

REGEX 正则表达式

var msg="@Mr.EasyBB";
msg.replace(/@(.+?)/g,
'<a href=\"http://'+window.location.host+'/u=$1\">$1</a>');

can someone help with this small issue results look like this 有人可以帮助解决这个小问题的结果

<a href="http://google.com">@U</a>sername

.+? is lazy matching - this will match as few characters as it can. 是懒惰匹配 - 这将匹配尽可能少的字符。

Try this. 尝试这个。 It will match as many non-whitespace characters as it can. 它将匹配尽可能多的非空白字符。

/@(\S+)/

试试这个:

msg = msg.replace(/@(\S+)/g, '<a href=\"http://'+window.location.host+'/u=$1\">@$1</a>');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM