3

I'm almost there! Just can't figure out the last part of what I need... forming the array.

I want to go through an html file and extract url's from between the phrases playVideo(' and ')

For testing purposes I am just trying to get it to work on the variable str, eventually this will be replaced by the html document:

<script type="text/javascript">
    var str ="playVideo('url1') BREAK playVideo('url2') BREAK playVideo('url3')"; 

    var testRE = str.match("playVideo\\(\'(.*?)\'");

    alert(testRE[1]);
</script>

This will output 'url1' but if I change it to alert(testRE[2]) it is undefined. How can I get it to fill out the array with all of the URLs (eg testRE[2] output 'url2' and so on) ?

Thanks for any help, new to regex.

3
  • stackoverflow.com/questions/6825492/… Commented Jan 31, 2012 at 0:48
  • Thanks for the link Ben but I don't see how that applies. I won't know what string I'm looking for since they will all eventually be various urls, so how could I indexOf for an unknown string? Commented Jan 31, 2012 at 0:59
  • Read the page--you look for that playVideo part of the string, it places you right at the start of each url you are looking for in succession. Commented Jan 31, 2012 at 5:06

2 Answers 2

3

Cannot comment, why is that, but adding that by iterating on the regex you get access to the groups;

var str ="playVideo('url1') BREAK playVideo('url2') BREAK playVideo('url3')";
var re = /playVideo\('(.*?)'\)/g;
while (match = re.exec(str)) {
    alert(match[1]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much carl. I'll go read up on exec. much appreciated.
2

Normally a javascript regular expression will just perform the first match. Update your regular expression to add the g modifier. Unfortunately JavaScript regular expressions don't have a non-capturing group so you have to do a little more processing to extract the bit you want e.g.

<script type="text/javascript">
var str ="playVideo('url1') BREAK playVideo('url2') BREAK playVideo('url3')"; 

var testRE = str.match(/playVideo\(\'[^']*\'/g);
var urls = [];
for (var i = 0; i < testRE.length; i++)
{
    urls[i] = testRE[i].substring(11).match(/[^']*/);
}

alert(urls[1]);
alert(urls[2]);
</script>

4 Comments

this is so close! only problem is now it is outputting "playvideo('url1" for testRE[0] and "playvideo('url2" for testRE[1] whereas I just want "url1" and "url2"
@Dougie Bear - If its working so far, try /playVideo\('([^']*)'/g or /playVideo\(\'([^']*)\'/g
those both still leave in playvideo(' which I need to scrap so that only the url remains
Thanks @Dougie Bear. Carl Krig's answer is a useful alternative. Instead of using the string.match() function you can use the RegExp.exec() function and iterate through the matches.

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.