Question

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)(.)?(-->)');
Was it helpful?

Solution 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)

Regex

(<[^>]*)(USERNAME)([^>]*>)


Demo

http://regex101.com/r/lI8cV8

OTHER TIPS

The correct expression appears to be

/<!--(.(?!-->))*USERNAME.*?-->/g
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top