[英]regular expression to match <script type=“text/javascript”> but not <script type=“text/html”>
当前代码(不起作用):
/^script\s*type=\"text\/javascript/i.test(tagName)
/<script\stype\=\"text\/javascript\">/i
Regular expressions and HTML — bad things. 正则表达式和HTML –不好的事情。 How regex will work for next examples (don't forget about single and double quotes)? 正则表达式将如何用于下一个示例(不要忘记单引号和双引号)?
<script type="text/javascript">
<script language="JavaScript" type="text/javascript">
<script type="text/javascript" language="JavaScript">
<script class="myJS" type="text/javascript">
<script type="text/javascript" class="myJS" >
Instead of regular expressions, I suggest to use a function like this: 建议不要使用正则表达式,而应使用如下函数:
function attr_in_str(str, tag, attr) {
var div = document.createElement('div');
div.innerHTML = str;
var elems = div.getElementsByTagName(tag);
for (var i = 0; i < elems.length; i++) {
if (elems[i].type.toLowerCase() == attr.toLowerCase()) {
return true;
}
}
return false;
}
Then use it: 然后使用它:
var str = 'This is my HTML <script type="text/javascript"></script>';
var result = attr_in_str(str, 'script', 'text/javascript');
Assuming that you are aware of all the assumption when you use regex to process HTML. 假设您在使用正则表达式处理HTML时已了解所有假设。
You can just remove the ^
in your current code, since it matches the start of the string. 您只需删除当前代码中的^
,因为它与字符串的开头匹配。
EDIT 编辑
Number of spaces should be at least 1, so your should change the *
after \\s
into +
空格数应至少为1,因此您应将\\s
之后的*
更改为+
I'm not a big fan of regex, so I'd do this: 我不是正则表达式的忠实拥护者,所以我会这样做:
var temp = document.createElement('div');
temp.innerHTML = '<script type="text/html"></script>';
var type = temp.childNodes[0].getAttribute('type');
if (type == 'text/javascript') {
// ...
}
If you were using jQuery, it would be way easier: 如果你使用jQuery,这将是比较容易的方式 :
if ($('<script type="text/html"></script>').prop('type') == 'text/javascript') {
// ...
}
To account for the 'type' appearing anywhere within the script tag, and multiple versions of quotes, use: 要说明出现在脚本标记内任何地方的“类型”以及多种引号,请使用:
/<script.*?type\s*=\s*.text\/javascript./i
You could tighten it up by specifying all quote alternatives instead of '.'. 您可以通过指定所有引号代替“。”来加强它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.