有一些字符串(例如,'s'):
import 'lodash';
// TODO import jquery
//import 'jquery';
/*
Some very important comment
*/
我该如何从字符串“s”中删除所有注释?应该使用正则表达式吗?我不知道。如何从字符串“s”中删除所有注释?是否需要使用正则表达式?我不确定。
有一些字符串(例如,'s'):
import 'lodash';
// TODO import jquery
//import 'jquery';
/*
Some very important comment
*/
我该如何从字符串“s”中删除所有注释?应该使用正则表达式吗?我不知道。MarcoS的答案在某些情况下无法使用...
以下是我的解决方案:
str.replace(/\/\*[\s\S]*?\*\/|(?<=[^:])\/\/.*|^\/\/.*/g,'');
从字符串中移除注释:
function removeComments(string){
//Takes a string of code, not an actual function.
return string.replace(/\/\*[\s\S]*?\*\/|(?<=[^:])\/\/.*|^\/\/.*/g,'').trim();//Strip comments
}
const commentedcode = `
alert('hello, this code has comments!')//An alert
/* A block comment here */
// A single line comment on a newline
`;
console.log(removeComments(commentedcode));
演示:
编辑: 我更改了公式以避免链接问题(参见评论)
\
const x = '// hello \n/hello/'`.replace(//*[\s\S]*?*/|//.*/g,'');will get
const x = '\n'` - lisonge如果您想使用正则表达式,可以使用以下表达式:
/(\/\*[^*]*\*\/)|(\/\/[^*]*)/
这段代码可以去除两种类型的注释:// ... \n
和 /* ... */
。
var stringWithoutComments = s.replace(/(\/\*[^*]*\*\/)|(\/\/[^*]*)/g, '');
console.log(stringWithoutComments);
var s = `before
/* first line of comment
second line of comment */
after`;
var stringWithoutComments = s.replace(/(\/\*[^*]*\*\/)|(\/\/[^*]*)/g, '');
console.log(stringWithoutComments);
输出:
before
after
// console.log(1*2)
- blackmiaool"ok = '// hi';".replace(/(\/\*[^*]*\*\/)|(\/\/[^*]*)/g, '');
will give "ok = '"
- nonopolarityconsole.log(`
var myStr = 'я! This \\'seems\\' to be a // comment'; // but this is actually the real comment.
/* like this one */ var butNot = 'this "/*one*/"'; // but this one and /* this one */
/* and */ var notThis = "one '//but' \\"also\\""; /* // this one */
`
// 1) replace "/" in quotes with non-printable ASCII '\1' char
.replace(/("([^\\"]|\\")*")|('([^\\']|\\')*')/g, (m) => m.replace(/\//g, '\1'))
// 2) clear comments
.replace(/(\/\*[^*]+\*\/)|(\/\/[^\n]+)/g, '')
// 3) restore "/" in quotes
.replace(/\1/g, '/')
);
//
,但我真的很喜欢它支持忽略引号内的注释。如果你用被接受的答案替换第二步,这会更好。 - phyatt(\/\*[\wа-я\'\s\r\n\*]*\*\/)|(\/\/[\wа-я\s\'\;]*)|(\<![\-\-\s\wа-я\>\/]*\>)
正则表达式部分:
Part1: (\/\*[\wа-я\'\s\r\n\*]*\*\/) for comments style: /* ..... */
Part2: (\/\/[\wа-я\s\'\;]*) for comments style: // .....
Part3: (\<![\-\-\s\wа-я\>\/]*\>) for comments style: <!-- ..... -->
更新的JsFiddle演示,支持在注释中使用俄罗斯符号
textarea{
width:300px;
height:120px;
}
<textarea id="code">
import 'lodash';
// TODO импортируем auth provider
//import 'jquery';
/*
Some very important comment
*/
</textarea>
<br />
<button onclick="removeAllComments()">Remove All Comments</button>
<script>
function removeAllComments(){
var str = document.getElementById('code').innerHTML.replace(/(\/\*[\wа-я\'\s\r\n\*]*\*\/)|(\/\/[\wа-я\s\'\;]*)|(\<![\-\-\s\wа-я\>\/]*\>)/ig, "");
document.getElementById('code').innerHTML = str;
}
</script>
comment1 = ' I dont like oneline comment. to parsing. // like this comment.'
comment2 = ' also i hate multiple line comment
/*
like
this.*/'
comment1.replace(/\s*(?:\/\/).*?$/gm , '')
// but you can't delete multiple line commet with regular grammar. like comment2
RegExp基于正则语法。这意味着正则语法解析器是一个有限状态机,无法保存状态,因此regexp不能像多行注释一样删除,只能使用单行注释。
如果您想删除多行注释,则必须编写解析器或使用其他工具而不是regexp。
我觉得这个非常有用且简洁的解决方案。
const word = `
/** @page type
* page = ".page-body"
* popup = ".pop-up-body"
* overlay = ".overlay-body" // new classes
*
*/
return {
"static" : '.page-body, .overlay-body, .pop-up-body, .card-wrapper { margin : 5px !important } .row { padding: 5px !important}'
}
`
console.log(word.replace(/\/*[^*]*.[^]*\//g,''))