![](/img/trans.png)
[英]Remove comments from string with JavaScript using JavaScript
[英]Regex to remove comments from SQL Query String using JavaScript
我設法找到用於處理/ * * /情況的正則表達式,但不適用於-情況。 如何更改我的正則表達式以解決此問題?
var s = `SELECT * FROM TABLE_A
/* first line of comment
second line of comment */
-- remove this comment too
SELECT * FROM TABLE_B`;
var stringWithoutComments = s.replace(/(\/\*[^*]*\*\/)|(\/\/[^*]*)|(--[^*]*)/g, '');
/*
Expected:
SELECT * FROM TABLE_A
SELECT * FROM TABLE_B
*/
console.log(stringWithoutComments);
謝謝
var s = `SELECT * FROM TABLE_A /* first line of comment second line of comment */ -- remove this comment too SELECT * FROM TABLE_B`; var stringWithoutComments = s.replace(/(\\/\\*[^*]*\\*\\/)|(\\/\\/[^*]*)|(--[^.].*)/gm, ''); /* Expected: SELECT * FROM TABLE_A SELECT * FROM TABLE_B */ console.log(stringWithoutComments); // without linebreak stringWithoutComments = stringWithoutComments.replace(/^\\s*\\n/gm, "") console.log(stringWithoutComments); // without whitespace stringWithoutComments = stringWithoutComments.replace(/^\\s+/gm, "") console.log(stringWithoutComments);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.