I get this error: Cannot call method 'replace' of undefined at String.toJadenCase
I get this error: Cannot call method 'replace' of undefined at String.toJadenCase
String.prototype.toJadenCase = function (str) {
//...
var capitalize = str;
return capitalize.replace(/^[a-zA-Z]*$/, function(txt){
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
当我将字符串 "How can mirrors be real if our eyes aren't real" 作为参数传递时,出现错误。它应该 return 每个单词大写,如:"How Can Mirrors Be Real If Our Eyes Aren't Real"。
我是 JS 和一般编程的新手,所以这可能是微不足道的。
toJadenCase
方法在 String
的上下文中 运行,因此使用 this
关键字检索文本。您还需要 fiddle 使用您的正则表达式:
String.prototype.toJadenCase = function () {
return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
var copy = "How can mirrors be real if our eyes aren't real";
alert(copy.toJadenCase());
请注意,这可以优雅地处理您的逗号。
由于您的函数需要一个参数,因此调用它的方式是:
myStr.toJadenCase(myStr);
这不是你想要的。
但是,如果您改用 this
,它将起作用:
return this.replace(/^[a-zA-Z]*$/, function(txt){
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
(这消除了错误,但您的大小写更改代码未按预期工作)
您可以使用 this
而无需参数。正则表达式 \b
匹配单词边界处的字符。
String.prototype.toJadenCase = function () {
return this.replace(/\b./g, function(m){
return m.toUpperCase();
});
}
正则表达式不处理特殊情况,如 aren't
。您必须匹配 space 后跟一个字符。为此,您可以改用
String.prototype.toJadenCase = function () {
return this.replace(/\s./g, function(m){
return m.toUpperCase();
});
}
或者更具体地说,您可以使用 /\s[a-zA-Z]/g
。
您可以看到正在运行的正则表达式 here。
用法
str = "How can mirrors be real if our eyes aren't real";
console.log(str.toJadenCase());
你想让它使用 this
,而且你的正则表达式是错误的。
function () {
return this.replace(/\b[a-zA-Z]*\b/g, function(txt){
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
我将正则表达式更改为使用单词分隔符,并且是全局的。
String.prototype.toJadenCase = function (str) {
//...
var capitalize = str;
return capitalize.replace(/^[a-zA-Z]*$/, function(txt){
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
当我将字符串 "How can mirrors be real if our eyes aren't real" 作为参数传递时,出现错误。它应该 return 每个单词大写,如:"How Can Mirrors Be Real If Our Eyes Aren't Real"。
我是 JS 和一般编程的新手,所以这可能是微不足道的。
toJadenCase
方法在 String
的上下文中 运行,因此使用 this
关键字检索文本。您还需要 fiddle 使用您的正则表达式:
String.prototype.toJadenCase = function () {
return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
var copy = "How can mirrors be real if our eyes aren't real";
alert(copy.toJadenCase());
请注意,这可以优雅地处理您的逗号。
由于您的函数需要一个参数,因此调用它的方式是:
myStr.toJadenCase(myStr);
这不是你想要的。
但是,如果您改用 this
,它将起作用:
return this.replace(/^[a-zA-Z]*$/, function(txt){
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
(这消除了错误,但您的大小写更改代码未按预期工作)
您可以使用 this
而无需参数。正则表达式 \b
匹配单词边界处的字符。
String.prototype.toJadenCase = function () {
return this.replace(/\b./g, function(m){
return m.toUpperCase();
});
}
正则表达式不处理特殊情况,如 aren't
。您必须匹配 space 后跟一个字符。为此,您可以改用
String.prototype.toJadenCase = function () {
return this.replace(/\s./g, function(m){
return m.toUpperCase();
});
}
或者更具体地说,您可以使用 /\s[a-zA-Z]/g
。
您可以看到正在运行的正则表达式 here。
用法
str = "How can mirrors be real if our eyes aren't real";
console.log(str.toJadenCase());
你想让它使用 this
,而且你的正则表达式是错误的。
function () {
return this.replace(/\b[a-zA-Z]*\b/g, function(txt){
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
我将正则表达式更改为使用单词分隔符,并且是全局的。