Caynadian
Caynadian

Reputation: 757

JavaScript Regex Match Index

I have the following little block of code to find an image source in a web page:

var reImg = [
  'src=\"(.*?)\" class=\"attachment-img-',
  'src=\"(.*?)\" class=\"image attachment-img-',
  'src=\"(.*?)-[0-9]+x[0-9]+(.*?)\" class=\"attachment-img-',
  'src=\"(.*?)\" class=\"full-size-gal'
];
var u = new RegExp(reImg.join('|'), 'i').exec(pageSource);
if (u) { 
  print(u);
}

I use an array to store the regexes because I may need to add more expressions later. Also, the double-quotes are escaped because the code above will be stored in a JSON file.

Now, the variable u has the results in it, but I need to get the value of u[1] or u[3] or u[5] - basically the stuff within the parenthesis. The problem is that I don't know which one matched. How do I get just the source URL?

Upvotes: 0

Views: 404

Answers (2)

Caynadian
Caynadian

Reputation: 757

While the solution from @PaulS does work, it required me to have an ever expanding if statement as more regexs are added to the list. Instead, I went with the suggestion by @CrayonViolent and used a for loop:

var reImg = [
  'src=\"(.*?)\" class=\"attachment-img-',
  'src=\"(.*?)\" class=\"image attachment-img-',
  'src=\"(.*?)-[0-9]+x[0-9]+(.*?)\" class=\"attachment-img-',
  'src=\"(.*?)\" class=\"full-size-gal'
];
for (var i=0;i<reImg.length;i++) {
  var u = new RegExp(reImg[i], 'i').exec(pageSource);
  if (u) { 
    print(u[1]);
    break;
  }
}

Upvotes: 1

Paul S.
Paul S.

Reputation: 66304

Just logical OR them with ||, the failed matches will be undefined, finish with || '' for an empty string match (the only possible falsy outcome in a match)

var re = /(a)|(b)|(c)/,
    m  = re.exec('foo_b_foo');
m;            // ["b", undefined, "b", undefined]
m[1] || m[2] || m[3]; // "b"

Remember that no match found will still be null so this should be inside an if block


var x;
if (u) { 
    x = u[1] || u[3] || u[5] || '';
    // these may not be all the possibilities from the example posted
    // i.e. you didn't mention `u[2]` ?
}

Upvotes: 0

Related Questions