如何使用正则表达式在字符串中找到@username(How find @username in string using regex)
如何使用javascript和regex在给定字符串中找到@username并将它们移动到div?
允许的项目:
@username @user_name不允许的项目:
@_username // non alphabetical character at the beginning @1username // a number at the beginning @username_ // underscore at the end我想把它们包装成div。 我尝试了什么: https : //regex101.com/r/olqjsv/2
How to find @username in given string using javascript and regex and move them to a div ?
Allowed Items:
@username @user_nameNot Allowed Items:
@_username // non alphabetical character at the beginning @1username // a number at the beginning @username_ // underscore at the endI want to wrap them in a div. What i tried: https://regex101.com/r/olqjsv/2
最满意答案
使用具有特定正则表达式模式的String.prototype.replace()函数的解决方案:
var str = "@username @username_ @user_name @1username", result = str.replace(/@[a-z]\w+[a-z]\b/gi, "<div>$&</div>"); console.log(result);$& - 特殊替换模式,指向匹配的子字符串
The solution using String.prototype.replace() function with specific regex pattern:
var str = "@username @username_ @user_name @1username", result = str.replace(/@[a-z]\w+[a-z]\b/gi, "<div>$&</div>"); console.log(result);$& - special replacement pattern, points to the matched substring
更多推荐
发布评论