如何循环所有与正则表达式匹配的元素? [英] How to loop all the elements that match the regex?

查看:640
本文介绍了如何循环所有与正则表达式匹配的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是这种情况:
我想找到与正则表达式匹配的元素...

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


targetText = SomeT1extSomeT2extSomeT3extSomeT4extSomeT5extSomeT6ext

targetText = "SomeT1extSomeT2extSomeT3extSomeT4extSomeT5extSomeT6ext"

我在这样的javascript中使用正则表达式

and I use the regex in javascript like this

reg = new RegExp(/e(.*?)e/g);   
var result = reg.exec(targetText);

我只获得第一个,但不是以下....
我只能获得T1,但不能获得T2,T3 ......

and I only get the first one, but not the follow.... I can get the T1 only, but not T2, T3 ... ...

推荐答案

var reg = /e(.*?)e/g;
var result;
while((result = reg.exec(targetText)) !== null) {
    doSomethingWith(result);
}

这篇关于如何循环所有与正则表达式匹配的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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