正则表达式删除多行注释 [英] Regex to remove multi line comments

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

问题描述

我正在尝试使用此正则表达式(JS):

I am trying to use this regex (JS):

/\/\*(.*)\*\//g

要替换

/*
sdandsads
*/

一无所有.

但是它不起作用!为什么?o_O

But it is not working! Why? o_O

推荐答案

该点捕获除换行符以外的所有内容..(如果dotall为假)

the dot catches everything except newlines..( if the dotall is false )

因此请使用dotall(如其他答案/注释中所述的,这在javascript中不受支持,但我将其留在此处以供参考)

so either use the dotall ( as mentioned in other answers/comments this is not supported in javascript, but i will leave it here for reference )

/\/\*(.*)\*\//gs

或在表达式中添加空格字符 \ s

or add the whitespace chars \s in your expressions

/\/\*((\s|.)*?)\*\//g

艾伦(Alan)在他的评论中提到我给出的答案表现不佳,因此请改用以下内容..(转换为所有空格,所有非空格都转换为.. )

Alan mentioned in his comment a bad performance from the answer i gave so use the following instead.. ( which translates to everything whitespace and everything non whitespace, so everything.. )

/\/\*([\s\S]*?)\*\//g

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

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