Reputation: 5605
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() );
});
Upvotes: 2
Views: 37
Reputation: 12024
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)
Upvotes: 1
Reputation: 9693
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>
Upvotes: 1
Reputation: 388446
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>
Upvotes: 1
Reputation: 13158
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);
Upvotes: 0