Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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/sharepoint/4.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中大写名称_Javascript_Typescript - Fatal编程技术网

在javascript中大写名称

在javascript中大写名称,javascript,typescript,Javascript,Typescript,我有一个使句子大写的函数。但它不能大写名称,例如 D'agostino, Fred D'agostino, Ralph B. D'allonnes, C. Revault D'amanda, Christopher 我期待着: D'Agostino, Fred D'Agostino, Ralph B. D'Allonnes, C. Revault D'Amanda, Christopher 功能: getCapitalized(str){ var smal

我有一个使句子大写的函数。但它不能大写名称,例如

D'agostino, Fred  
D'agostino, Ralph B.  
D'allonnes, C. Revault  
D'amanda, Christopher 
我期待着:

D'Agostino, Fred  
D'Agostino, Ralph B.  
D'Allonnes, C. Revault  
D'Amanda, Christopher 
功能

getCapitalized(str){
    var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;
    return str.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 + 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);
    });
  }
getCapitalized(str){
var smallWords=/^(a | an | and | as | at |但是| by | en | for | if | in | nor | of | on |或| per | the | to | vs | | | | via)$/i;
返回str.replace(/[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(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);
});
}

有人能帮我弄清楚这个问题吗?我已经尝试过使用
(title.charAt(index+match.length)!=“'””“| | title.charAt(index-1)==“””)
,但没有任何帮助

我不确定您需要注意的所有用例,但是对于您提出的问题,您可以使用查找单词边界的正则表达式:

函数名称(名称){
返回name.replace(/\b(\w)/g,s=>s.toUpperCase());
}
log(大写名称(`D'agostino,Fred`));
log(大写名称(`D'agostino,Ralph B.`));
log(大写名称(`D'allonnes,C.Revault`);

log(大写名称(`D'amanda,Christopher`)我使用此函数将名称大写。 该参数用于在大写之前强制使用小写,否则会得到奇怪的结果(BaLlERinO、LaMbORGhini..)

它使用regexp在['`'.-]中查找空格或特殊字符 后跟任何不在ASCII组0-97和123-223中的字符,因为只有符号、数字和大写字母,但此字符后面不得后跟其他空格或字符串结尾(以避免出现类似Ciccio的结果)


您的代码中测试
的部分当前是否仅适用于与
smallWords
匹配的内容?噢,我现在看到了!谢谢@nnnnnn@nnnnnn你对此有什么最佳解决方案吗?最佳解决方案是:。没有办法制作一个能正确处理所有名称的程序。让用户输入他们想要的名称,然后不要再碰它们。更不用说我找到了解决问题的方法<代码>str.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\'\s-]*/g,
刚刚在正则表达式数组中添加了
\'
String.prototype.capitalize = function (lower) {
  return (lower ? this.toLowerCase() : this).replace(/(?:^|\s|['`‘’.-])[^\x00-\x60^\x7B-\xDF](?!(\s|$))/g, function (a) {
    return a.toUpperCase();
  });
};

console.log(' ëkthor thörsen's örst'ûber o'brian von-fist's'.capitalize(true)
Ëkthor Thörsen's Örst'Ûber O'Brian Von-Fist's