您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

toTitleCase.js 907B

123456789101112131415161718192021
  1. /**
  2. * To Title Case 2.1 - http://individed.com/code/to-title-case/
  3. * Copyright 2008-2013 David Gouch. Licensed under the MIT License.
  4. * https://github.com/gouch/to-title-case
  5. */
  6. import trim from './trim';
  7. var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i; // test
  8. export default function toTitleCase(string) {
  9. return trim(string).replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function (match, index, title) {
  10. 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) {
  11. return match.toLowerCase();
  12. }
  13. if (match.substr(1).search(/[A-Z]|\../) > -1) {
  14. return match;
  15. }
  16. return match.charAt(0).toUpperCase() + match.substr(1);
  17. });
  18. }