I use the below technique with things like HTML, XML and similar, (even though it is bad practice when it comes to nested content)
Regex
(<[^>]*)(USERNAME)([^>]*>)
質問
Let's say I have the following text :
foo bar baz <!-- USERNAME --> <!-- ACCESS_KEY --> baz bar foo
<!--:USERNAME-->
<!-- eueuteh -->
-DarchetypeVersion=<!-- java-version --> -UserName=<!-- :USERNAME --> -DsauceAccessKey=<!-- ACCESS_KEY -->
What I want to do is replace <!-- USERNAME -->
with some other text. Unfortunately the regex I've created doesn't actually work as intended, and for example replaces the whole <!-- java-version --> -UserName=<!-- :USERNAME -->
expression. The first two occurences are replaced properly. Where is the mistake ?
Here's what I've got, testing in javascript console :
var re = new RegExp('(<!--)(.)*(USERNAME)(.)?(-->)');
解決 2
I use the below technique with things like HTML, XML and similar, (even though it is bad practice when it comes to nested content)
(<[^>]*)(USERNAME)([^>]*>)
他のヒント
The correct expression appears to be
/<!--(.(?!-->))*USERNAME.*?-->/g