[英]How do I upper and sentence case with two buttons in JavaScript
I have two buttons, one for upper case and another for sentence case, but the upper case button is not not working.我有两个按钮,一个用于大写,另一个用于句子,但大写按钮不起作用。 My code is below.
我的代码如下。 What am I doing wrong?
我究竟做错了什么?
function firstLetterUpper(theString) { var newString = theString.toLowerCase().replace(/([az]+)|([AZ]+)/g, function(c) { return c.toUpperCase() }); return newString; } function convertToSentenceCase() { var theString = document.testForm.theString.value; //alert(theString); var newString = firstLetterUpper(theString); //console.log("Converted: "+newString); document.getElementById('theString').value = newString; } function firstLetterUpper(theString) { var newString = theString.toLowerCase().replace(/(^\\s*\\w|[\\.\\!\\?]\\s*\\w)/g, function(c) { return c.toUpperCase() }); return newString; } function convertToSentenceCase1() { var theString = document.testForm.theString.value; //alert(theString); var newString = firstLetterUpper(theString); //console.log("Converted: "+newString); document.getElementById('theString').value = newString; }
<form name="testForm"> <textarea name="theString" id="theString" cols="50" rows="5">converts text string to "sentence case" -- capitalizes the first letter of every sentence, and makes the rest lower case. helpful when you need to convert content that is in all caps.</textarea><br> <button onclick="convertToSentenceCase();return false;">Convert to Sentence Case</button> <button onclick="convertToSentenceCase1();return false;">Convert to Upper Case</button> </form>
You have the same name for both of your functions firstLetterUpper
.您的两个函数
firstLetterUpper
具有相同的名称。
function firstLetterUpper(theString) { var newString = theString.toLowerCase().replace(/([az]+)|([AZ]+)/g, function(c) { return c.toUpperCase() }); return newString; } function convertToSentenceCase() { var theString = document.testForm.theString.value; //alert(theString); var newString = firstLetterUpper(theString); //console.log("Converted: "+newString); document.getElementById('theString').value = newString; } function firstLetterUpper1(theString) { var newString = theString.toLowerCase().replace(/(^\\s*\\w|[\\.\\!\\?]\\s*\\w)/g, function(c) { return c.toUpperCase() }); return newString; } function convertToSentenceCase1() { var theString = document.testForm.theString.value; //alert(theString); var newString = firstLetterUpper1(theString); //console.log("Converted: "+newString); document.getElementById('theString').value = newString; }
<form name="testForm"> <textarea name="theString" id="theString" cols="50" rows="5">converts text string to "sentence case" -- capitalizes the first letter of every sentence, and makes the rest lower case. helpful when you need to convert content that is in all caps.</textarea><br> <button onclick="convertToSentenceCase();return false;">Convert to Sentence Case</button> <button onclick="convertToSentenceCase1();return false;">Convert to Upper Case</button> </form>
Not sure if this is what you wanted.不确定这是否是您想要的。 But to make a string uppercase you can use JavaScript's in-built toUpperCase() function.
但是要使字符串大写,您可以使用 JavaScript 的内置 toUpperCase() 函数。
If you change your function to this it works as expected.如果您将函数更改为此,它会按预期工作。
function convertToSentenceCase1() {
var theString = document.testForm.theString.value;
//alert(theString);
var newString = theString.toUpperCase();
//console.log("Converted: "+newString);
document.getElementById('theString').value = newString;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.