I am hoping to remove all comments from a DNS zone file with JavaScript. The comments start with a semicolon (;) in a DNS zone files. However, I also don't want the semicolons being replaced if they are preceded by a back slash. I have some test code which looks like this:
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..."
The code above works as expected. However, the following code replaces one more character than expected:
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
In the second example, what I expected is test 300 IN A 100.100.100.100. However, it returns test 300 IN A 100.100.100.10. So what's wrong with my regular expression?