2

Is it possible to convert the next character after each "_" sign in to capitals?

eg:

var myString = "Taylor_swift";
var rg = /(_)/gi;
myString = myString.replace(rg, function(toReplace) {
    $("#textField").val( toReplace.toUpperCase() );
});
Becky
  • 5,217
  • 9
  • 33
  • 68

4 Answers4

1

You can try

var myString = "Taylor_swift";
var rg = /_(.)/gi;
myString = myString.replace(rg, function(match, toReplace) {
  return ' ' + toReplace.toUpperCase();
});
console.log(myString);
$('#result').html(myString)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="result"></div>
Arun P Johny
  • 376,738
  • 64
  • 519
  • 520
1

You can create a custom javascript function

function myUpperCase(str) {
  if (str.indexOf("_") >= 0) {
var res = str.split("_");
res[0] = res[0].toUpperCase();
str = res.join("_");
  }
  return str;
}

var myString = "Taylor_swift";
var upCase = myUpperCase(myString);

$("#result").html(upCase);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="result"></div>
rorra
  • 9,403
  • 3
  • 37
  • 60
1

Try also:

var txt = "Taylor_swift_more_text".replace(/[^_]+/g, function(a){
   return a.charAt(0).toUpperCase() + a.substr(1)
}).replace(/_+/g," ");

document.write(txt)
0

Yes, you can use the split method on the string:

myWords = myString.split('_');
myWords[1] = myWords[1].toUpperCase();

To capitalize only the first letter, use this:

myWords[1] = myWords[1].charAt(0).toUpperCase() + myWords[1].slice(1);
Brent Washburne
  • 12,113
  • 4
  • 57
  • 77