user1663023
user1663023

Reputation:

Remove comments with replace in JavaScript

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?

Upvotes: 3

Views: 91

Answers (2)

topsoftwarepro
topsoftwarepro

Reputation: 822

This too removes all comments using replace.

$("#doc").keyup(function(evt) {
  var doc = $("#doc").val();
  var documentNew = String(doc).replace(/(\r\n|\n|\r)/gm, "\n");
  var str = documentNew;
  var lines = str.split('\n');

  var filterred = lines.filter(function(line) {
    return line.indexOf('//') != 0;
  });

  filterred = String(filterred).replace(/;/g, "; \n");

  $("#answerDocument2").html(filterred);
});
textarea {
  width: 200px;
  height: 200px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h3>Paste Code</h3>
<textarea id="doc"></textarea>
<h3>Copy Formatted Code</h3>
<textarea id="answerDocument2" readonly style="resize: none;"></textarea>

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626893

You need to make sure the ; is not preceded with a \, thus, use a (^|[^\\]) group before ; and replace using a callback:

var re = /(^|[^\\]);.*/g; 
var str = 'example.com IN TXT "v=DKIM1\\; k=rsa\\; p=MIGf..."\ntest 300 IN A 100.100.100.100;this is a comment';
var result = str.replace(re, function(m, g1){
  return g1 ? g1 : ""; // if g1 is set/matched, re-insert it, else remove 
});
document.body.innerHTML = result.replace(/\n/g, "<br/>"); // just for display

The regex matches:

  • (^|[^\\]) - (capture group #1) start of string (^) or (|) any character but a \ ([^\\])
  • ; - a literal ;
  • .* - zero or more characters other than a newline

Upvotes: 3

Related Questions