如何将句号后的第一个字母大写?
例如,如果在文本框中输入名称,如 j.daniel Pigott
或 j. daniel Pigott
它应该反射(reflect)为 J.Daniel Pigott
并删除空格。我可以在打字时将第一个字母大写,如何从下面的代码中将句号后的字母更改为大写。
<asp:TextBox ID="tbxName" runat="server" CssClass="tbxCandidateInfo" TabIndex="1"></asp:TextBox>
$('#tbxName').keyup(function (event) {
var arrWord = $(this).val().split(' ');
var result = "";
for (var i = 0; i < arrWord.length; i++) {
result += arrWord[i].substring(0, 1).toUpperCase() + arrWord[i].substring(1).toLowerCase();
if (i < arrWord.length - 1) {
result += ' ';
}
}
$(this).val(result);
});
1) While user entering his name (any name) it should *Capitalize* first letter
2) If user entering his name along with initial then *Capitalize* the word after period.
3) Uppercase for entire words not allowed.
These changes should happen while entering this name field ***onkeyup*** event
最佳答案
我的建议是这样的:
console.log('example:')
console.log(
"j. delan pegot".replace(/\b\w/g, l => l.toUpperCase()).replace('. ', '.')
);
const stringCapitalize = (str) => {
return str.replace(/\b\w/g, l => l.toUpperCase()).replace('. ', '.')
}
const onChange = (evt) => {
const input = document.getElementById("input");
input.value = stringCapitalize(input.value);
}
<Label>Write something: </Label><input id="input" onkeyup="onChange()" />
您需要附加一个 onkeyup
函数来执行我上面描述的操作。尝试演示中的输入。
编辑: 要了解正则表达式,请参阅:
https://stackoverflow.com/questions/11874234/difference-between-w-and-b-regular-expression-meta-characters
关于javascript - 在 javascript live 中,在输入字段中写入时,在句号后大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50528153/