Developer Strange
Developer Strange

Reputation: 2198

use regex to remove external script tag

I tried to remove the external script tag from a string using a regular expression.

var data = "<p>Valid paragraph.</p><p>Another valid paragraph.</p><script>alert123</script><script type='text/javascript' src='123'>Dangerous scripting!!!</script><script type='text/javascript' src='123'/><p>Last final paragraph.</p>";

data.replace(/<script[^>]\(\src=\)*>(?:(?!<\/script>)[^])*<\/script>/g, "")

output should be

"<p>Valid paragraph.</p><p>Another valid paragraph.</p><script>alert123</script><p>Last final paragraph.</p>";

but it not validating. how to fix this issue?

Upvotes: 0

Views: 260

Answers (1)

here I tested a regex and I think it is what you're looking for:

(<script[^>].*<\/script>)|(<script[^>].*\/\s?>)

https://regexr.com/5n377 see the effect!

EDIT

(<script[^>].*?<\/script>)|(<script[^>].*\/\s?>)
            ^^^ here Edited

Upvotes: 1

Related Questions