Javascript中的正则表达式去除脚本标签等等


Regex expression in Javascript to strip script tags and further more

我想使用正则表达式来剥离 html 文本。我想摆脱所有的剧本,风格和<!--标签。

你能帮我弄清楚怎么做吗?

到目前为止,我得到的是以下功能,遗憾的是它不起作用。

console.log(doTheMagic('This is a test < !-- hello --> Bla bla'));
function doTheMagic(text){
text = text.replace(/(<!--.'/-->)/g, '');       
return text;
}

我发现 php 中的preg_replace函数表达式是这样的,但我无法让它在 javascript 中工作:

'@<script[^>]*?>.*?</script>@si',   /* strip out javascript */
        '@<['/'!]*?[^<>]*?>@si',            /* strip out HTML tags */
        '@<style[^>]*?>.*?</style>@siU',    /* strip style tags properly */
        '@<!['s'S]*?--[ 't'n'r]*>@'         /* strip multi-line comments */

任何帮助表示赞赏。

/(<!--.'/-->)/g会匹配<!--x/-->,请尝试使用.*而不是.,因为仅点就只匹配一个字符。

但我建议也阅读您得到的评论