最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Remove all scripts with javascript regex - Stack Overflow

programmeradmin3浏览0评论

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.

The code is:

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.

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.

The code is:

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.

Share Improve this question asked Apr 4, 2013 at 16:34 user1853892user1853892 1072 silver badges5 bronze badges 2
  • /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi – Prakash Chennupati Commented Apr 4, 2013 at 16:38
  • this is the good answer for me ! – David Auvray Commented Nov 14, 2017 at 16:06
Add a ment  | 

1 Answer 1

Reset to default 7

Try this:

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

or

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

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

发布评论

评论列表(0)

  1. 暂无评论