2016-10-13 00:16:48 点击量:54 标签: 收藏本文
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];
- })
- );
- }
我们是一家提供综合软件外包与弱电工程服务的技术公司。
业务涵盖定制软件、管理系统、网站、小程序、APP、系统集成、网络布线、监控安防、门禁、机房建设及长期技术维护。
公司地址:湖北省宜昌市伍家岗区东站二路6号三峡(宜昌)大数据产业园海洋馆二楼A203室