Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  7] [ 4]  / answers: 1 / hits: 28990  / 12 Years ago, mon, february 4, 2013, 12:00:00

JS Bin demo



This regex transform each lower case word to upper case. I have a full name input field. I do want the user to see that each word's first letter he/she pressed is converted to uppercase in the input field.



I have no idea how to properly replace the selected characters in the current input field.



$('input').on('keypress', function(event) {
var $this = $(this),
val = $this.val(),
regex = /b[a-z]/g;

val = val.toLowerCase().replace(regex, function(letter) {
return letter.toUpperCase();
});

// I want this value to be in the input field.
console.log(val);
});

More From » jquery

 Answers
40

Given i.e: const str = "hello world" to become Hello world


const firstUpper = str.substr(0, 1).toUpperCase() + str.substr(1);

or:


const firstUpper = str.charAt(0).toUpperCase() + str.substr(1);

or:


const firstUpper = str[0] + str.substr(1);

[#80434] Saturday, February 2, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazminuniquer

Total Points: 63
Total Questions: 121
Total Answers: 96

Location: Cambodia
Member since Thu, May 21, 2020
4 Years ago
;