javascript - 试图找到所有正则表达式匹配的索引,但有些被遗漏了
问题描述
我想在字符串中的第一个“e”之后找到每个元音的索引。
由于您无法直接从中获取捕获组的索引RegExp.exec(sInput)
,但您可以获得包含实际捕获组前面所有内容的捕获组的长度,因此我用来执行此操作的正则表达式是/(.*?e.*?)(a|e|i|o|u)(.*)/
.
所以设置基本上是这样的:
let re = /(.*?e.*?)(a|e|i|o|u)(.*)/g;
let sInput = "lorem ipsum";
let tMatches = [];
let tMatchIndices = [];
let iPrevIndex = 0;
while (result = re.exec(sInput)) {
/* result[0]: full match
result[1]: match for 1st capture group (.*?e.*?)
result[2]: match for 2nd capture group (a|e|i|o|u)
result[3]: match for 3rd capture group (.*)
*/
let index = result[1].length + iPrevIndex;
let sMatch = result[2];
tMatchIndices.push(index);
tMatches[index] = sMatch;
iPrevIndex = index + sMatch.length;
re.lastIndex = iPrevIndex;
}
for (i = 0; i < tMatches.length; i++) {
let index = tMatchIndices[i];
console.log(tMatches[index] + " at index "+index);
}
问题在于输入字符串“lorem ipsum”,我需要“i”和“u”的索引......它只给我“i”的索引。
我知道它为什么这样做 - 将搜索索引推进到第一个匹配之后会切断应该触发下一个匹配的“e”。我坚持的是如何解决它。我不能只是简单地不推进搜索索引,否则它永远不会超过第一个匹配项。
我曾考虑过在进行过程中简单地从搜索字符串中删除每个匹配项,但是随后将其后的每个字符的索引都向左移动,因此我收集的索引对于原始的未截断的索引甚至都不准确细绳。
做什么?
解决方案
您可以通过积极的回顾来做到这一点:
'lorem ipsum'.replace(/(?<=e.*)[aiueo]/g, function(m, offset) {
console.log(m + ' ==> ' + offset)
});
输出:
i ==> 6
u ==> 9
解释:
(?<=e.*)
- 性格的正面回顾e
[aiueo]
- 扫描元音- 使用
g
标志重复 - 在替换功能中,您可以参考偏移量
推荐阅读
- react-native - S3 Amplify React Native Storage.get(image) 不显示
- android - Android微调器选择位置与字节转换逻辑
- vba - 如果出现,如何查找和替换最后一个数字
- python - 不知道如何使用 JSON 从 api 中提取某些数据
- vue.js - Vue在绑定组件中显示道具
- reactjs - useRef - 使用 ref 来处理功能组件内的函数?
- javascript - 使用什么方法访问数组中的多个对象?
- docker - 将带有端口的 URL 映射到没有 Docker 端口的 URL?
- mysql - 编写 MySQL 查询以获得测试表的预期结果
- android - 如何将 Kotlin Multiplatform Mobile 与 Amazon 放大服务集成?