admin管理员组

文章数量:1356709

I am trying to create a regex that I can use to remove any closing ment syntax out of string.

For instance if I had:

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

Here is what I have so far:

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

An HTML markup example would be:

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

so the string would be help:: This is a ment -->

I am trying to create a regex that I can use to remove any closing ment syntax out of string.

For instance if I had:

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

Here is what I have so far:

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

An HTML markup example would be:

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

so the string would be help:: This is a ment -->

Share Improve this question edited May 30, 2015 at 4:30 Luca asked May 30, 2015 at 4:19 LucaLuca 3853 silver badges15 bronze badges 1
  • 1 When you say target major programming languages, are you meaning that the regex should be usable within those languages or simply that you want to catch the ments from major programming languages? – pcnate Commented May 30, 2015 at 4:44
Add a ment  | 

4 Answers 4

Reset to default 8

This should support many languages including bash which doesn't support \s:

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

You can also use Which prevents any un-necassary selection making this easier to use also:

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

You could use this as a funky .replace but it might result in quirky 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

You can add more languages as needed:

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

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

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

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

    var regExArray = [['\\/\\* help::','*/'],['<!-- help::','-->']]
var regexMatchers =  regExArray.map(function(item){
                        return new RegExp('^'+item[0]+'(.*)'+item[1]+'$')})

function RemoveEndingTagsNew(ment){
    var newComment;
    regexMatchers.forEach(function(regEx,index){
        if(regEx.test(ment)){
        newComment=ment.replace(/.* help::/,"").replace(regExArray[index][1],"")
       }
    });
    return newComment || ment;

}

Its longer version, but doesnt remove ments if starting and ending ment tags doesnt match.

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

本文标签: javascriptRegex to remove comments endingsStack Overflow