我要每个句子的首字母大写。我的代码只大写第一个字,第一个字符的段落。因此,我想大写第一个单词,第一个字符后的句号和感叹号。
示例(我想要这个)
代码:
Html代码:
<textarea autocomplete="off" cols="30" id="TextInput" name="message" oninput="myFunction()" rows="10" style="width: 100%;"></textarea>
<br><br>
<input id="FistWordFirstCharcterCapital" onclick="FistWordFirstCharcterCapital()" style="color: black;" type="button" value="First word first character capital of each sentence!" />Javascript代码
<script>
function FistWordFirstCharcterCapital() {
var string = document.getElementById("TextInput").value.toLowerCase();;
var x = string.replace(string[0], string[0].toUpperCase());
document.getElementById("TextInput").value = x;
}
</script>发布于 2020-08-09 11:06:59
你可以先试着把句子分开。然后,将它们映射到大写字母,如下所示:
function FistWordFirstCharcterCapital() {
var el = document.getElementById("TextInput");
el.value = el.value.split(/[.?!]/).map(str =>{
if(str.charAt(0) == ' ')
return ' ' + str.charAt(1).toUpperCase() + str.slice(2);
else
return str.charAt(0).toUpperCase() + str.slice(1);
}).join('.');
}<textarea autocomplete="off" cols="30" id="TextInput" name="message" rows="10" style="width: 100%;"></textarea>
<br><br>
<input id="FistWordFirstCharcterCapital" onclick="FistWordFirstCharcterCapital()" style="color: black;" type="button" value="First word first character capital of each sentence!" />
https://stackoverflow.com/questions/63325312
复制相似问题