如何在 JavaScript 中将出现在指定字符集合中的字符后面的字符大写?

huangapple go评论74阅读模式
英文:

How to capitalize a character that appears after a character from specified set of characters in JavaScript?

问题

function capitalizeChar() {
  var wordAfter = ["-", ":", "—", ".", "?", "!"];
  var c = false;
  var words = "welcome:  to the universe.";
  var charactercheck = words.split("");
  for (var i = 0; i < charactercheck.length; i++) {
    if (c == true) {
      if (charactercheck[i] != " ") {
        charactercheck[i] = charactercheck[i].toUpperCase();
        c = false;
      }
    }
    for (var j = 0; j < wordAfter.length; j++) {
      if (charactercheck[i] == wordAfter[j]) {
        c = true;
        break;
      }
    }
  }
  console.log(charactercheck.join(""));
}
capitalizeChar();

但出于某种原因,此代码似乎没有将冒号 ":" 后面的字母 "t" 大写。可以有人帮忙吗?

英文:

I wish to capitalize a first character of word that appears after a special character from specified set (array) is found in the string. Also I want ignore any number of spaces that it might have after the character from the array is found in the string but still capitalize the character after the special characters from the set.

Here is my code that I tried but not working:

function capitalizeChar() {
  var wordAfter = [&quot;-&quot;, &quot;:&quot;, &quot;—&quot;, &quot;.&quot;, &quot;?&quot;, &quot;!&quot;];
  var c = false;
  var words = &quot;welcome:  to the universe.&quot;;
  var charactercheck = words.split(&quot;&quot;);
  for (var i = 0; i &lt; charactercheck.length; i++){
  if (c == true ) {
    if (charactercheck[i]!= &quot;&quot;){
	  charactercheck[i] = charactercheck[i].toUpperCase(); 
	  c=false;  }
    }
	for (var j = 0; j &lt; wordAfter.length; j++) {
		if (charactercheck[i] == wordAfter[j]) {
		c = true; break;
		}
    }
  } 
  console.log(charactercheck.join(&quot;&quot;));
  }
capitalizeChar();

But for some reason this code does not seem to be capitalize the 't' of 'To' which appears after the colon ':'. Can someone help, please!

答案1

得分: 2

以下是翻译好的内容:

这是我使用Dimava的正则表达式解决的方法。但是Alexander的解决方案非常准确。

function capitalizeChar() {
  var words = "欢迎:来到宇宙。";
  console.log(words.replace(/([-:—.?!]\s*)(\w)/g, (s, a, b, c) => a + b.toUpperCase()));
}
capitalizeChar();
英文:

Here is how I solved it by using Dimava 's regex. But Alexander's solution was spot on.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function capitalizeChar() {
  var words = &quot;welcome:  to the universe.&quot;;
  console.log(words.replace(/([-:—.?!]\s*)(\w)/g, (s, a, b, c) =&gt; a + b.toUpperCase()));
}
capitalizeChar();

<!-- end snippet -->

答案2

得分: 0

function capitalizeChar(str, charAfter = ["-", ":", "—", ".", "?", "!"]) {
    const regex = new RegExp(`(?<=[${charAfter.join('')}]\\s*)[a-z]`, 'g');
    return str.replace(regex, s => s.toUpperCase());
}

console.log(capitalizeChar('welcome:  to the universe. and -enjoy your ! suffering.'));
英文:

Use a regular expression, should be pretty fast:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function capitalizeChar(str, charAfter = [&quot;-&quot;, &quot;:&quot;, &quot;—&quot;, &quot;.&quot;, &quot;?&quot;, &quot;!&quot;]) {
    const regex = new RegExp(`(?&lt;=[${charAfter.join(&#39;&#39;)}]\\s*)[a-z]`, &#39;g&#39;);
    return str.replace(regex, s =&gt; s.toUpperCase());
}

console.log(capitalizeChar(&#39;welcome:  to the universe. and -enjoy your ! suffering.&#39;));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月19日 18:27:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定