如何让<script>代码使用页面源中的正则表达式?</script>

时间:2012-03-12 01:12:07

标签: javascript regex

我想获取脚本代码,但外部引用脚本除外。 例如:我想得到以下代码:

<script type="text/javascript">alert("test");</script> 

从下一个s:

var s ='<script type="text/javascript" src="image/plupload/plupload.js"></script><script type="text/javascript" src="image/plupload/plupload.flash.js"></script><script type="text/javascript" src="image/plupload/plupload.html4.js"></script><script type="text/javascript" src="image/plupload/plupload.html5.js"></script><script type="text/javascript">alert("test");</script>'

p = /<(script)\s+((language=['"]?javascript['"]?)|(type=['"]?text\/javascript['"]?))?\s*\/?>.*(?:<\/\1>)?/gi;
var arr = new Array();
while(arr = p.exec(s)) 
alert(arr[1]+','+arr[1]+','+arr[2]);

但正则表达式错误,我无法得到正确的结果

2 个答案:

答案 0 :(得分:3)

Never parse HTML with regex. The <center> cannot hold.

为您的脚本添加一个唯一的id并使用document.getElementById(MyId).textContent,或者只使用document.getElementsByTagName('script')[i].textContent转发它们。

答案 1 :(得分:2)

但是使用正则表达式和javascript解析来自任意字符串的非嵌套脚本标记很容易:

var input = '<div>random html in here</div><script type="text/javascript" src="image/plupload/plupload.js"></script><script type="text/javascript" src="image/plupload/plupload.flash.js"></script><script type="text/javascript" src="image/plupload/plupload.html4.js"></script><script type="text/javascript" src="image/plupload/plupload.html5.js"><div>random html in here</div></script><script type="text/javascript">alert("test");</script><div>random html in here</div>';
var pattern = /<script[^>]+?>.*?<\/script>/gi; //whole thing.
var matches = input.match(pattern);
var result = "";
for (var i in matches) result += "Match:" + matches[i] + "\n";
alert(result);