[英]Split multiple words started with @ sign
var test = "Hello all, this is a message and i want to mention @john, @smith and @jane...";
而我想得到的是:
var result = ["john", "smith", "jane"];
我可以取字符串中的最后一个用户名,但不是全部。 我对正则表达式或其他字符串函数没问题。
谢谢。
试试这个正则表达式
/(^|\W)@\w+/g
JavaScript的:
var test = "Hello all, this is a message and i want to mention @john, @smith and @jane";
var names = test.match(/(^|\W)@\w+/g);
console.log(names);
结果:
0: "@john"
1: "@smith"
2: "@jane"
RegExr上的实例 : http: //regexr.com?36t6g
var test = "Hello all, this is a message and i want to mention @john, @smith and @jane...";
var patt = /(^|\s)@([^ ]*)/g;
var answer = test.match(patt)
应该得到你想要的
喜欢这个JSfiddle
似乎使用单个正则表达式是不可能的:
var result = test.match(/@\w+/g).join('').match(/\w+/g);
您可能需要处理正则表达式找不到的情况:
var result = test.match(/@\w+/g);
result = result ? result.join('').match(/\w+/g) : [];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.