Q:

js replace regex first letter to uppercase

I have XML content like this
<abc:content><bcd:position>Just text: node not need to replace</abc:content>

and I need replace it with
<abc:Content><bcd:Position>Just text: node not need to replace</abc:Content>

In SublimeText or Notepad++ I can replace it with regex, if I search for
:. or :\b(\w) or :\b.

and replace it with
:\U$1

It works fine. But I cant use
string.replace(/:\b./g , ':\U$1');

this dont work correctly! If I try use ':$1'.toUpperCase() its still dont give right result - This used in other questions, and dont work for me.
Help me, please!

A:

You can use a replacement function:

var xml = '<abc:content><bcd:position></abc:content>';

var xml2 = xml.replace(/:\w/g, function(matched) {
  return matched.toUpperCase();
});

console.log(xml2);

Bear in mind that using regex is not a very good idea here as it will also replace any other letters in your XML that come right after a colon:

var xml = '<abc:content><bcd:position>Just text:node not need to replace</bcd:position></abc:content>';

var xml2 = xml.replace(/:\w/g, function(matched) {
  return matched.toUpperCase();
});

console.log(xml2);

