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?
reImgandexecthem individually and return/break at first matchuagainst each eleme ofreImgafter the initial match (what you are doing now) so that you don't have to walk through all ofpageSourcefor each regexString.match, but evenString.matchworks if you only match once.