Regex to remove comments

I am trying to create a regex that I can use to remove the comment closing syntax from a string.

For example, if I had:

/* help::this is my comment */ should return this is my comment or <!-- help:: this is my other comment --> should return this is my other comment . Ideally, I would like to target all major programming languages ​​that require comment tags to end.

Here is what I still have:

 function RemoveEndingTags(comment){ return comment.split('help::')[1].replace("*/", "").replace("-->", ""); //my ugly solution } 

HTML markup example:

 <!-- help:: This is a comment --> <div>Hello World</div> 

so the line will be help:: This is a comment -->

+8
javascript regex
source share
4 answers

This should support many languages, including bash, which does not support \s :

 help::[\r\n\t\f ]*(.*?)[\r\n\t\f ]*?(?:\*\/|-->) 

You can also use one that prevents any unnecessary choices that make it easier to use. :

 help::[\r\n\t\f ]*(.*?)(?=[\r\n\t\f ]*?\*\/|[\r\n\t\f ]*?-->) 

You can use this as a funky .replace , but this can lead to fancy behavior:

 /\/\*[\r\n\t\f ]*help::|<!--[\r\n\t\f ]*help::|[\r\n\t\f ]\*\/|[\r\n\t\f ]*-->/g 

Explanation

Solution 1:

 help:: Matches the text "help::" [\r\n\t\f ]* Matches any whitespace character 0-unlimited times (.*?) Captures the text [\r\n\t\f ]*? Matches all whitespace (?: Start of non-capture group \*\/ Matches "*/" | OR --> Matches "-->" ) End non capture group 

[\r\n\t\f ]

 \r Carriage return \n Newline \t Tab \f Formfeed Space 

Solution 2 (supports almost everything)

 help:: Matches "help::" [\r\n\t\f ]* Matches all whitespace 0-unlimited (.*?) Captures all text until... (?= Start positive lookahead [\r\n\t\f ]*? Match whitespace 0-unlimited \*\/ Matches "*/" | OR [\r\n\t\f ]*? Match whitespace 0-unlimited --> Matches "-->" ) 

Demo 1

Demo 2

+8
source share

If necessary, you can add additional languages:

 help::.*?\s(.*)(?:.*?\s\*\/|.*?\s\-->) 

Example: https://regex101.com/r/rK2kU0/1

+3
source share
 var str = '<!-- A comment -->'; var newstr = str.replace(/<\!--(.*?)-->/, '$1'); console.log(newstr); // A comment 

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

+2
source share
  var regExArray = [['\\/\\* help::','*/'],['<!-- help::','-->']] var regexMatchers = regExArray.map(function(item){ return new RegExp('^'+item[0]+'(.*)'+item[1]+'$')}) function RemoveEndingTagsNew(comment){ var newComment; regexMatchers.forEach(function(regEx,index){ if(regEx.test(comment)){ newComment=comment.replace(/.* help::/,"").replace(regExArray[index][1],"") } }); return newComment || comment; } 

Its a longer version, but does not remove comments if the start and end comment tags do not match.

Demo: https://jsfiddle.net/6bbxzyjg/2/

+2
source share

All Articles