3
我希望能夠使用JavaScript從DNS區域文件中刪除所有註釋。註釋在DNS區域文件中以分號(;
)開頭。不過,我也不希望分號前面加上反斜槓。我有一些如下所示的測試代碼:刪除JavaScript中替換的註釋
var record = 'example.com IN TXT "v=DKIM1\\; k=rsa\\; p=MIGf..."';
var recordWithCommentRemoved = record.replace(/[^\\];[\s\S]*?$/gm, '');
console.log(recordWithCommentRemoved); // example.com IN TXT "v=DKIM1\; k=rsa\; p=MIGf..."
上述代碼正常工作。但是,下面的代碼代替一個或多個字符比預期:
var record = 'test 300 IN A 100.100.100.100;this is a comment';
var recordWithCommentRemoved = record.replace(/[^\\];[\s\S]*?$/gm, '');
console.log(recordWithCommentRemoved); // test 300 IN A 100.100.100.10
在第二個例子,我的預期是test 300 IN A 100.100.100.100
。但是,它返回test 300 IN A 100.100.100.10
。那麼我的正則表達式有什麼問題?