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 -->
- 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
4 Answers
Reset to default 8This 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
版权声明:本文标题:javascript - Regex to remove comments endings - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744024356a2577779.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论