Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
71
rated 0 times [  78] [ 7]  / answers: 1 / hits: 48866  / 15 Years ago, thu, september 17, 2009, 12:00:00

I'm trying to return the contents of any tags in a body of text. I'm currently using the following expression, but it only captures the contents of the first tag and ignores any others after that.



Here's a sample of the html:



    <script type=text/javascript>
alert('1');
</script>

<div>Test</div>

<script type=text/javascript>
alert('2');
</script>


My regex looks like this:



//scripttext contains the sample
re = /<scriptb[^>]*>([sS]*?)</script>/gm;
var scripts = re.exec(scripttext);


When I run this on IE6, it returns 2 matches. The first containing the full tag, the 2nd containing alert('1').



When I run it on http://www.pagecolumn.com/tool/regtest.htm it gives me 2 results, each containing the script tags only.


More From » regex

 Answers
23

The problem here is in how exec works. It matches only first occurrence, but stores current index (i.e. caret position) in lastIndex property of a regex. To get all matches simply apply regex to the string until it fails to match (this is a pretty common way to do it):



var scripttext = ' <script type=text/javascript>nalert('1');n</script>nn<div>Test</div>nn<script type=text/javascript>nalert('2');n</script>';

var re = /<scriptb[^>]*>([sS]*?)</script>/gm;

var match;
while (match = re.exec(scripttext)) {
// full match is in match[0], whereas captured groups are in ...[1], ...[2], etc.
console.log(match[1]);
}

[#98669] Tuesday, September 15, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arturo

Total Points: 331
Total Questions: 99
Total Answers: 92

Location: Austria
Member since Thu, Jan 7, 2021
3 Years ago
;