简体   繁体   English

如何在 JavaScript 中使用两个按钮进行大写和句子大小写

[英]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.

相关问题 如何在JavaScript中将用户输入转换为大写? - How do i turn user input into upper case in JavaScript? Javascript 识别句子,无论句子中的任何位置大小写 - Javascript recognize sentence regardless of upper and lower case anywhere in the sentence 如何让 JavaScript 的 replaceAll() 将带有小写和大写的字符串视为小写? - How do I make JavaScript's replaceAll() treat strings with both lower case and upper case as if they were lower case? 如何使用简单的子字符串功能为javascript中的句子加上大小写? - How do I title case a sentence in javascript using simple substring function? 如何在Javascript中为句子加标题 - How to Title Case a sentence in Javascript 如何使用javascript一键切换大写和小写 - How do you use one button to switch upper case and lower case by using javascript JavaScript 正则表达式如何处理这个脊柱案例句子 - How JavaScript regex work on this spinal case sentence 在这种情况下如何使用 JavaScript 句型? - How to use JavaScript sentence cases in this case? 如何用 Javascript 中的文本替换这两个按钮 - How to I replace these two buttons with a text in Javascript (Javascript)搜索特定项目时如何忽略在搜索栏中注册的小写/大写 - (Javascript) How do you ignore lower/upper case being registered in search bars when searching for a specific item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM