英文:
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 = ["-", ":", "—", ".", "?", "!"];
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();
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 = "welcome: to the universe.";
console.log(words.replace(/([-:—.?!]\s*)(\w)/g, (s, a, b, c) => 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 = ["-", ":", "—", ".", "?", "!"]) {
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.'));
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论