javascript - 需要 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)
按要求返回。我可以在这里做什么?
解决方案
您可以简化您的正则表达式:
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);
推荐阅读
- for-loop - C 优化方法
- javascript - 在 Angular 生产构建期间出现错误
- grails - 使用带有对象 ID 列表的动态查找器
- ios - 使用 UIGraphicsImageRenderer 渲染全尺寸图像
- php - 如何在 PHP 中抑制 SonarLint 警告?
- javascript - 我如何以编程方式调整铯承诺方法中元素的高度。
- python - 通过 groupby 连接 pandas 数据框
- android - MvvmCross 和 Android 通知
- javascript - CSS可以通过像素映射来变形图像吗?
- javascript - “Web 的交互式数据可视化”中的回调函数变量顺序