首页 > 解决方案 > 需要 javascript 正则表达式,仅括号中的数字,多个

问题描述

对于更新/问题解决方案向下滚动 或点击这里

原版。问题:我想用纯 javascript 编写一个函数,它使用带有 :eq(0) 声明的类似 Jquery 的选择器,让我们说例如遍历一个像这样的表

$('tr:eq(8) > td:eq(13)').val();

为此,我正在尝试编写一个正则表达式,但我被困在这里

var str = "I don't want +5+ and not 10 or [12] nor (120) but :eq(5) and :eq(120)";
var matches = str.match(/\:eq\(\d+(\d{1,2})\)?/g);
console.log('matches: '+matches);

它只返回matches: :eq(120)但不是matches: :eq(5),:eq(120)按要求返回。我可以在这里做什么?

标签: javascriptregexmatch

解决方案


您可以简化您的正则表达式:

var str = "I don't want +5+ and not 10 or [12] nor (120) but :eq(5) and :eq(120)";
const matches = str.match(/:eq\(\d+\)/g);
console.log('matches: '+matches);


推荐阅读