Javascript RegEx-通过字符串解析特定单词
我希望通过一个字符串进行解析,找到所有句柄(@name),并将它们分别放入一个数组(但不带Javascript RegEx-通过字符串解析特定单词,javascript,regex,Javascript,Regex,我希望通过一个字符串进行解析,找到所有句柄(@name),并将它们分别放入一个数组(但不带@),这样我就可以循环遍历它们(使用forEach),并向每个句柄发送警报。每个手柄由一个空格分隔 试试看 让str=“这里@ann和@john去@jane”; 设m=str.match(/@\w+/g).map(x=>x.replace(/./,''); m、 forEach(x=>console.log(x))您可以捕获@followedByName然后替换@ let str=`@someName嘿
@
),这样我就可以循环遍历它们(使用forEach
),并向每个句柄发送警报。每个手柄由一个空格分隔
试试看
让str=“这里@ann和@john去@jane”;
设m=str.match(/@\w+/g).map(x=>x.replace(/./,'');
m、 forEach(x=>console.log(x))
您可以捕获@followedByName
然后替换@
let str=`@someName嘿@someMoreNames`
让op=str.match(/(^ |\s)@\w+/g).map(e=>e.trim().replace(/@/g'))
console.log(op)
如果您只需要从tweet中提取用户,可以使用以下正则表达式:
/@([a-zA-Z0-9]+)/g
例如:
var string = '@JohnSmith @DylanThompson Hey guys!';
var numberPattern = /@([a-zA-Z0-9]+)/g;
var res = string.match(numberPattern);
console.log(res);
这将表明:
["@JohnSmith", "@DylanThompson"]
您可以组合使用
match
提取名称,并使用slice
删除@
:
str=“@JohnSmith@DylanThompson嘿伙计们”;
设arr=str.match(/@\w+/g).map(e=>e.slice(1));
控制台日志(arr)代码>请尝试以下操作:
var str=“这里@ann和@john去@jane”;
变量patt=/@(\w+)/g;
虽然((arr=patt.exec(str))!==null){console.log(arr[1]);}
您能添加您迄今为止尝试过的内容吗?我现在在Firefox中打开网页时出错了,但在Chrome中仍然可以正常工作。Firefox错误是:SyntaxError:invalid regexp group