JavaScript regular expression with global match help needed

后端 未结 3 709
孤独总比滥情好
孤独总比滥情好 2021-01-13 15:45

I\'m new to JavaScript and have a question about regular expressions. I have the following code:

var patt3=new RegExp(/(July|August)\\s+\\d{1,2}(\\s|,)\\d{4}         


        
3条回答
  •  伪装坚强ぢ
    2021-01-13 16:16

    g only matches all occurrences of the entire pattern. It is not analogous to matching groups like PHP's preg_match_all. Without g, it only matches the first occurrence of the pattern (matches[0]) and all matched groups are the other array elements.

    If you wanted to get nested matches as well, you can use the concepts described by John Resig to do so:

    str3.replace(patt3, function () {
        //This is the first captured group in all matches iterated over
        console.log(arguments[1]);
    });
    

提交回复
热议问题