使用javascript正则表达式删除所有脚本 [英] Remove all scripts with javascript regex

查看:96
本文介绍了使用javascript正则表达式删除所有脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用javascript中的regex从下面类型的字符串中删除包含内容的所有脚本标记。
但我仍然得到输出:

I am trying to remove all scripts tags with content from the string of the type below with regex in javascript. But I am still getting as output:

");</script>

当它应该是空字符串时。

when it should be an empty string.

代码是:

var BG = '<script type="text/javascript">document.write("<script type=\"text\/javascript\" src=\"http:\/\/site;js=y;target=_blank;time="+ (window.emediate_time ? window.emediate_time : window.emediate_time = new Date().getTime()) +"1053997930;"><\/script>");</script><script type="text/javascript" src="some?cre=mu;js=y;target=_blank"></script>';

BG = BG.replace(/<\s*script.*?>.*?(<\s*\/script.*?>|$)/ig,'');

你能否告诉我有什么问题以及如何解决。谢谢。

Could you please tell me what's wrong and how to fix it. Thanks.

推荐答案

试试这个:

(/<.*?script.*?>.*?<\/.*?script.*?>/igm, '')

(/<script.*?>.*?<\/script>/igm, '')

(你需要'm'切换到搜索多行)

(you need 'm' switch to search multi-line)

这篇关于使用javascript正则表达式删除所有脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆