简体   繁体   English

在字符串中查找所有匹配的正则表达式模式和匹配索引

[英]Find all matching regex patterns and index of the match in the string

I want to find /AA/ pattern in AA-AA-AA subject string.我想在AA-AA-AA主题字符串中找到/AA/模式。 I need to get the matching string and the position (index) of the match.我需要获取匹配的字符串和匹配的 position(索引)。

I have looked at RegExp.prototype.exec() .我看过RegExp.prototype.exec() It only returns the first match:它只返回第一个匹配项:

/AA/g.exec('AA-AA-AA')

exec() only returns a single match. exec()只返回一个匹配项。 To get all matches with a g lobal regexp, you have to call it repeatedly, eg.:要使用全局正则表达式获取所有匹配项,您必须重复调用它, g

var match, indexes= [];
while (match= r.exec(value))
    indexes.push([match.index, match.index+match[0].length]);

Be careful when using RegExp.prototype.exec() function to match a string.使用RegExp.prototype.exec() function 匹配字符串时要小心。 The constructed regex object is stateful, ie every time you call .exec() it affects the lastIndex property of the regex instance.构造的正则表达式 object 是有状态的,即每次调用.exec()它都会影响正则表达式实例的lastIndex属性 Therefore, you should always reset the lastIndex property before using an instance of regex object.因此,在使用正则表达式 object 的实例之前,您应该始终重置lastIndex属性。

let re,
    findAAs;

re = /AA/;

findAAs = (input) => {
    let match;

    // `re` is cached instance of regex object.
    // Reset `re.lastIndex` every time before using it.

    re.lastIndex = 0;

    while ((match = re.exec(input)) !== null) {
        match.index; // Match index.
        match[0]; // Matching string.
    }
};

A tempting alternative is to construct the regex object on every execution.一个诱人的替代方法是在每次执行时构造正则表达式 object。 Depending on how resource intensive your task is, this is an option too.根据您的任务的资源密集程度,这也是一个选项。

let findAAs;

findAAs = (input) => {
    let match,
        re;

    re = /AA/;

    while ((match = re.exec(input)) !== null) {
        match.index; // Match index.
        match[0]; // Matching string.
    }
};

A pragmatic alternative to using .exec() is String.prototype.replace() .使用.exec()的实用替代方法是String.prototype.replace()

let findAAs,
    re;

re = /AA/;

findAAs = (input) => {
    let match,
        re;

    input.replace(re, (match, index) => {
        match; // Matching string.
        index; // Match index.

        return '';
    });
};

The downside of this approach is that it constructs a copy of the subject string.这种方法的缺点是它构造了主题字符串的副本。

Whether you should use it or not, depends on how resource intensive your task is.您是否应该使用它,取决于您的任务的资源密集程度。 Personally, I like to avoid while blocks in my code and therefore prefer the .replace() approach.就个人而言,我喜欢在我的代码中避免使用while块,因此更喜欢.replace()方法。

http://jsfiddle.net/mplungjan/MNXvQ/ http://jsfiddle.net/mplungjan/MNXvQ/

I think this is easier to grasp我认为这更容易掌握

var str = "AAbAAcAAd"
var re = /(AA)/gi;
var t="",cnt=0;
while ((result=re.exec(str))!=null) {
    document.write((cnt++)+":"+result[1]+"<br />")        
}

re.lastIndex contains the positions each time re.lastIndex 包含每次的位置

An alternative to bobince's answer is to use the 'lastIndex' property of the regex to get the end index of each match bobince 答案的替代方法是使用正则表达式的“lastIndex”属性来获取每个匹配项的结束索引

var match, indexes= [];
while (match = r.exec(value)) {
    indexes.push([match.index, r.lastIndex]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM