Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting Strings Into an Array Regex

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.

like image 255
dougmacklin Avatar asked Aug 03 '26 05:08

dougmacklin


1 Answers

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]);
}
like image 65
Carl Krig Avatar answered Aug 05 '26 19:08

Carl Krig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!