Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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
Javascript 如何使用正则表达式在字符串中查找@username_Javascript_Regex - Fatal编程技术网

Javascript 如何使用正则表达式在字符串中查找@username

Javascript 如何使用正则表达式在字符串中查找@username,javascript,regex,Javascript,Regex,如何使用javascript和正则表达式在给定字符串中查找@username,并将它们移动到div 允许的项目: @username @user_name @_username // non alphabetical character at the beginning @1username // a number at the beginning @username_ // underscore at the end 不允许的项目: @username @user_name @_user

如何使用javascript和正则表达式在给定字符串中查找
@username
,并将它们移动到div

允许的项目:

@username
@user_name
@_username // non alphabetical character at the beginning
@1username // a number at the beginning
@username_ // underscore at the end
不允许的项目:

@username
@user_name
@_username // non alphabetical character at the beginning
@1username // a number at the beginning
@username_ // underscore at the end
我想把它们包在一个小盒子里。 我试过的:

试一下

(\@username|\@user_name)|\@([a-zA-Z]+)\_\@([a-zA-Z]+)
使用具有特定正则表达式模式的函数的解决方案:

var str=“@username@username@user\u name@1username”,
结果=str.replace(/@[a-z]\w+[a-z]\b/gi,“$&”);

控制台日志(结果)在末尾放一个
\b