How to loop all the elements that match the regex?

前端 未结 8 1534
囚心锁ツ
囚心锁ツ 2020-12-02 18:27

Here is the case: I want to find the elements which match the regex...

targetText = \"SomeT1extSomeT2extSomeT3extSomeT4extSomeT5extSomeT6ext\"

8条回答
  •  旧巷少年郎
    2020-12-02 18:55

    You could also use the String.replace method to loop through all elements.

    result = [];
     // Just get all numbers
    "SomeT1extSomeT2extSomeT3ext".replace(/(\d+?)/g, function(wholeMatch, num) {
      // act here or after the loop...
      console.log(result.push(num));
      return wholeMatch;
    });
    console.log(result); // ['1', '2', '3']
    

    Greetings

提交回复
热议问题