簡體   English   中英

正則表達式使用JavaScript從SQL查詢字符串中刪除注釋

[英]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);

謝謝

https://jsfiddle.net/8fuz7sxd/1/

 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM