亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

MMTTMM 2021-10-07 10:47:01
我設法找到了用于處理 /* */ 案例的正則表達式,但它不適用于 -- 案例。如何更改我的正則表達式來解決這個問題?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_ASELECT * FROM TABLE_B*/console.log(stringWithoutComments);謝謝https://jsfiddle.net/8fuz7sxd/1/
查看完整描述

1 回答

?
茅侃侃

TA貢獻1842條經驗 獲得超22個贊

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);


查看完整回答
反對 回復 2021-10-07
  • 1 回答
  • 0 關注
  • 272 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號