3

How can I use an array of Regex expressions and iterate that array with 'exec' operation. I did initialize an array with various regular expressions like this:

var arrRegex = new Array(/(http:\/\/(?:.*)\/)/g, /(http:\/\/(?:.*)\/)/g);

Now I created a for loop that does this:

 for(i=0;i<arrRegex.length;i++){
     arrRegex[i].exec(somestring);
 }

The thing is that this doesn't seems to work. I don't want to use it hardcoded like this:

  (/(http:\/\/(?:.*)\/)/g).exec(somestring);

When using the array option, the '.exec' function returns null. When I use the hardcoded option it returns the matches as I wanted.

1
  • the exec doesn't return the matches as it should, but with the hard coded it does. matches = null Commented Jul 15, 2012 at 11:37

2 Answers 2

2

The exec() returns the match so you should be able to capture it.

somestring = 'http://stackoverflow.com/questions/11491489/iterate-through-regular-expression-array-in-javascript';
var arrRegex = new Array(/(http:\/\/(?:.*)\/)/g, /(http:\/\/(?:.*)\/)/g);
for (i = 0; i < arrRegex.length; i++) {
    match = arrRegex[i].exec(somestring);
}

match is an array, with the following structure:

{
    [0] = 'string matched by the regex'
    [1] = 'match from the first capturing group'
    [2] = 'match from the second capturing group'
    ... and so on
}

Take a look at this jsFiddle http://jsfiddle.net/HHKs2/1/

You can also use test() instead of exec() as a shorthand for exec() != null. test() will return a boolean variable depending on whether the regex matches part of the string or not.

Sign up to request clarification or add additional context in comments.

5 Comments

I know that, and it worked beautifully when hardcoded, but returns null when I use the regex in the array like you wrote in your code. I use it on chrome browser
Take a look at the jsFiddle link I posted, it uses the array way and returns results. Or are you referring to initialising the expressions from a string? Btw, you can also use test() instead of exec() as a shorthand for exec() != null. test() will return a boolean variable depending on whether the regex matches part of the string or not.
if what you are saying is right, how come test here returns false: jsfiddle.net/IdanShechter/uLMBL
@Dareen: It's important to note that match only retruns match groups if the regex is non-global. If the regex is global, it returns an array of whole matches only.
@IdanShechter: Thats because the exec() modifies the properties of the regular expression object. Note that if you remove it, test() will return true: jsfiddle.net/uLMBL/1
2

What you probably want to do is to capture the first group:

for(i=0;i<arrRegex.length;i++){
  var someotherstring = arrRegex[i].exec(somestring)[1];
  // do something with it ...
}

BTW: That is my guess, not sure what you are trying to do. But if you are trying to get the host name of a URL you should use /(http:\/\/(?:.?)\/)/g. The question mark after .* makes the previous quantifier (*) ungreedy.

2 Comments

how come the test() function returns false, it should return true because there are matches jsfiddle.net/IdanShechter/uLMBL
Well, your code is working here - even in Chrome. Could you give us a bit more context, like the left hand assignment of the exec() result. Or you could just provide your the actual relevant code you are working on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.