javascript - 如果第一个字符不匹配,则 regexp exec 内部索引不会进展 [英] javascript - regexp exec internal index doesn't progress if first char is not a match

查看:19
本文介绍了javascript - 如果第一个字符不匹配,则 regexp exec 内部索引不会进展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要匹配组中没有以/"开头的数字.

I need to match numbers that are not preceeded by "/" in a group.

为了做到这一点,我做了以下正则表达式:

In order to do this I made the following regex:

var reg = /(^|[^,\/])([0-9]*\.?[0-9]*)/g;

第一部分匹配字符串的开头和除/"以外的任何内容,第二部分匹配一个数字.关于正则表达式一切正常(它符合我的需要).我使用 https://regex101.com/ 进行测试.此处示例:https://regex101.com/r/7UwEUn/1

First part matches start of the string and anything else except "/", second part matches a number. Everything works ok regarding the regex (it matches what I need). I use https://regex101.com/ for testing. Example here: https://regex101.com/r/7UwEUn/1

问题是,当我在 js(下面的脚本)中使用它时,如果字符串的第一个字符不是数字,它会进入无限循环.仔细一看,它似乎一直在匹配字符串的开头,不再继续.

The problem is that when I use it in js (script below) it goes into an infinite loop if first character of the string is not a number. At a closer look it seems to keep matching the start of the string, never progressing further.

 var reg = /(^|[^,\/])([0-9]*\.?[0-9]*)/g;
 var text = "a 1 b";
 while (match = reg.exec(text)) {
     if (typeof match[2] != 'undefined' && match[2] != '') {
         numbers.push({'index': match.index + match[1].length, 'value': match[2]});
     }
 }

如果字符串以数字开头 ("1 a b") 一切正常.

If the string starts with a number ("1 a b") all is fine.

问题似乎出在这里 (^|[^,/]) - 删除 ^|将解决无限循环的问题,但它不符合我在以数字开头的字符串中所需的内容.

The problem appears to be here (^|[^,/]) - removing ^| will fix the issue with infinite loop but it will not match what I need in strings starting with numbers.

知道为什么内部索引没有进展吗?

Any idea why the internal index is not progressing?

推荐答案

无限循环是由您的正则表达式可以匹配空字符串这一事实引起的.您不太可能需要空字符串(即使根据您的代码判断),因此使其至少匹配一位数字,将最后一个 * 替换为 +:

Infinite loop is caused by the fact your regex can match an empty string. You are not likely to need empty strings (even judging by your code), so make it match at least one digit, replace the last * with +:

var reg = /(^|[^,\/])([0-9]*\.?[0-9]+)/g; 
var text = "a 1 b a 2 ana 1/2 are mere (55";
var numbers=[];
while (match = reg.exec(text)) {
    numbers.push({'index': match.index + match[1].length, 'value': match[2]});
 }
console.log(numbers);

请注意,此正则表达式不会匹配 34. 之类的数字,在这种情况下,您可以使用 /(^|[^,\/])([0-9]*\.?[0-9]+|[0-9]*\.)/g,见这个正则表达式演示.

Note that this regex will not match numbers like 34. and in that case you may use /(^|[^,\/])([0-9]*\.?[0-9]+|[0-9]*\.)/g, see this regex demo.

或者,您可以使用另一个技巧",在不匹配时手动推进正则表达式 lastIndex:

Alternatively, you may use another "trick", advance the regex lastIndex manually upon no match:

var reg = /(^|[^,\/])([0-9]*\.?[0-9]+)/g;
 var text = "a 1 b a 2 ana 1/2 are mere (55";
 var numbers=[];
 while (match = reg.exec(text)) {
    if (match.index === reg.lastIndex) {
        reg.lastIndex++;
    }
    if (match[2]) numbers.push({'index': match.index + match[1].length, 'value': match[2]});
 }
 console.log(numbers);

这篇关于javascript - 如果第一个字符不匹配,则 regexp exec 内部索引不会进展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆