js删除一段代码中的所有注释内容
js 如何删除一段代码中的所有注释内容
js 如何删除一段代码中的所有注释内容
- function removeJsComments(code) {
- return code.replace(/(?:^|\n|\r)\s*\/\*[\s\S]*?\*\/\s*(?:\r|\n|$)/g, '\n').replace(/(?:^|\n|\r)\s*\/\/.*(?:\r|\n|$)/g, '\n');
- }
- function removeComments(str) {
- var uid = '_' + +new Date(),
- primatives = [],
- primIndex = 0;
- return (
- str
- /* Remove strings */
- .replace(/(['"])(\\\1|.)+?\1/g, function(match){
- primatives[primIndex] = match;
- return (uid + '') + primIndex++;
- })
- /* Remove Regexes */
- .replace(/([^\/])(\/(?!\*|\/)(\\\/|.)+?\/[gim]{0,3})/g, function(match, $1, $2){
- primatives[primIndex] = $2;
- return $1 + (uid + '') + primIndex++;
- })
- /*
- - Remove single-line comments that contain would-be multi-line delimiters
- E.g. // Comment /* <--
- - Remove multi-line comments that contain would be single-line delimiters
- E.g. /* // <--
- */
- .replace(/\/\/.*?\/?\*.+?(?=\n|\r|$)|\/\*[\s\S]*?\/\/[\s\S]*?\*\//g, '')
- /*
- Remove single and multi-line comments,
- no consideration of inner-contents
- */
- .replace(/\/\/.+?(?=\n|\r|$)|\/\*[\s\S]+?\*\//g, '')
- /*
- Remove multi-line comments that have a replaced ending (string/regex)
- Greedy, so no inner strings/regexes will stop it.
- */
- .replace(RegExp('\\/\\*[\\s\\S]+' + uid + '\\d+', 'g'), '')
- /* Bring back strings & regexes */
- .replace(RegExp(uid + '(\\d+)', 'g'), function(match, n){
- return primatives[n];
- })
- );
- }
热门文章推荐
- [JS]window.location获取url各项参数详解
- [JS]jQuery,javascript获得网页的高度和宽度
- [JS]视频弹窗视频弹出层videoLightBox(含三种播放器的用法)
- [JS]JS提交中文encodeURI两次转码
- [JS]js版方面encodeURI转码和decodeURI解码的用法实例
- [JS]js取当前机子的时间戳实例
- [JS]AES加密(基于crypto-js)PHP后端解密
- [JS]data:image/png;base64写法的用途及说明
请稍候...