[英]Remove dash between strings and Capitalize the String in Javascript
I am trying to capitalize the String after removing all dashes in between. 我正在尝试在删除其间的所有破折号后大写字符串。
so this i-am-string
would become I am string
. 所以这个i-am-string
将成为I am string
。
This is what I tried, but it does capitalize, but I don't know how to remove dashes and capitalize. 这是我尝试过的,但它确实大写,但我不知道如何删除破折号和大写。
function tweakFunction (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
Thanks 谢谢
You could use a couple regex, like in the PHP version you previously posted: 你可以使用一些正则表达式,就像你之前发布的PHP版本一样:
var result = str
.replace(/-/g, ' ')
.replace(/^./, function(x){return x.toUpperCase()})
function tweakFunction(str) {
str = str.replace(/-/g, ' ');
return str.charAt(0).toUpperCase() + str.slice(1);
}
console.log(tweakFunction('i-am-string')); //=> "I am string"
/* Capitalize the first letter of the string and the rest of it to be Lowercase */
function capitalize(word){
return word.charAt(0).toUpperCase() + word.substring(1).toLowerCase()
}
console.log(capitalize("john")); //John
console.log(capitalize("BRAVO")); //Bravo
console.log(capitalize("BLAne")); //Blane
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.