Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.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 从复杂字符串中提取文本_Javascript_Regex - Fatal编程技术网

Javascript 从复杂字符串中提取文本

Javascript 从复杂字符串中提取文本,javascript,regex,Javascript,Regex,我的字符串包含 str=“这是一个测试字符串@[test_](89)和@[test_v](91),让我们试试。” 我正在提取 usernames=str.match(/([^“@\[])[^@\[]+?(?=\]\\\\\\([1-9]+[0-9]*\)+/g);/[“test\u”,“test\u v”] 我需要以类似的方式提取id,并且需要[“89”,“91”] 我有不完整的正则表达式,如ids=str.match(/([^“(])[^@\(]+?(?=\)+/g); 请建议使用正则表达式提

我的字符串包含

str=“这是一个测试字符串@[test_](89)和@[test_v](91),让我们试试。”

我正在提取

usernames=str.match(/([^“@\[])[^@\[]+?(?=\]\\\\\\([1-9]+[0-9]*\)+/g);/[“test\u”,“test\u v”]

我需要以类似的方式提取id,并且需要
[“89”,“91”]

我有不完整的正则表达式,如
ids=str.match(/([^“(])[^@\(]+?(?=\)+/g);

请建议使用正则表达式提取id


谢谢

您可以使用捕获组提取它们:

var rx=/\[([^\][]+)]\(\d+)\/g;
var s=“这是一个测试字符串@[test_](89)和@[test_v](91),让我们试试。”
var键=[],id=[],m;
while(m=rx.exec){
按键。按(m[1]);
id.push(m[2]);
}
控制台日志(键);

console.log(ids)
谢谢@Wiktor Stribiżew