Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript toTitleCase功能工作不正常_Javascript_Jquery - Fatal编程技术网

Javascript toTitleCase功能工作不正常

Javascript toTitleCase功能工作不正常,javascript,jquery,Javascript,Jquery,我试图将此字符串转换为正确的大小写,但它不会返回正确的大小写。知道出了什么问题吗?(我没有错误) var convert=“到此为止”; String.prototype.toTitleCase=函数(){ var smallWords=/^(a | an | and | as | at |但是| by | en | for | if | in | nor | of | on |或| per | the | to | vs | | | | via)$/i; 返回此。替换(/[A-Za-z0-9\

我试图将此字符串转换为正确的大小写,但它不会返回正确的大小写。知道出了什么问题吗?(我没有错误)

var convert=“到此为止”;
String.prototype.toTitleCase=函数(){
var smallWords=/^(a | an | and | as | at |但是| by | en | for | if | in | nor | of | on |或| per | the | to | vs | | | | via)$/i;
返回此。替换(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g,函数(匹配,索引,标题){
如果(索引>0&&index+match.length!==title.length&&
match.search(smallWords)>-1&&title.charAt(索引-2)!==“:”&&
(title.charAt(index+match.length)!='-'| | title.charAt(index-1)='-')&&
title.charAt(索引-1).search(/[^\s-]/)<0){
返回match.toLowerCase();
}
if(match.substr(1).search(/[A-Z]\../)>-1){
复赛;
}
返回match.charAt(0.toUpperCase()+match.substr(1);
});
};
convert.toTitleCase();
警报(转换);

此行
convert.toTitleCase()正在丢弃结果。该方法正在返回正确的结果,但您没有对其执行任何操作

var original = "this is the end";

String.prototype.toTitleCase = function () {
    var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;

    return this.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function (match, index, title) {
        if (index > 0 && index + match.length !== title.length &&
  match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
  (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
  title.charAt(index - 1).search(/[^\s-]/) < 0) {
            return match.toLowerCase();
        }

        if (match.substr(1).search(/[A-Z]|\../) > -1) {
            return match;
        }

        return match.charAt(0).toUpperCase() + match.substr(1);
    });
};

var titleCased = original.toTitleCase();

alert(titleCased);
var original=“到此为止”;
String.prototype.toTitleCase=函数(){
var smallWords=/^(a | an | and | as | at |但是| by | en | for | if | in | nor | of | on |或| per | the | to | vs | | | | via)$/i;
返回此。替换(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g,函数(匹配,索引,标题){
如果(索引>0&&index+match.length!==title.length&&
match.search(smallWords)>-1&&title.charAt(索引-2)!==“:”&&
(title.charAt(index+match.length)!='-'| | title.charAt(index-1)='-')&&
title.charAt(索引-1).search(/[^\s-]/)<0){
返回match.toLowerCase();
}
if(match.substr(1).search(/[A-Z]\../)>-1){
复赛;
}
返回match.charAt(0.toUpperCase()+match.substr(1);
});
};
var titlecase=original.toTitleCase();
警报(标题化);

您是否尝试过一步一步地调试?仅供参考,“smallWords”通常被称为“Stop Words”:可能是因为您返回了一个新字符串。试试
convert=convert.toTitleCase()
为什么要编写自己的版本?!为什么?
var original = "this is the end";

String.prototype.toTitleCase = function () {
    var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;

    return this.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function (match, index, title) {
        if (index > 0 && index + match.length !== title.length &&
  match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
  (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
  title.charAt(index - 1).search(/[^\s-]/) < 0) {
            return match.toLowerCase();
        }

        if (match.substr(1).search(/[A-Z]|\../) > -1) {
            return match;
        }

        return match.charAt(0).toUpperCase() + match.substr(1);
    });
};

var titleCased = original.toTitleCase();

alert(titleCased);