在字符串中大写单词的最佳方法是什么?


当前回答

Jquery或Javascipt没有提供内置方法来实现这一点。

CSS测试转换(text-transform:capitalize;)并不真正大写字符串的数据,而是在屏幕上显示一个大写的渲染。

如果您正在使用普通的香草ajs在数据级别上寻找一种更合法的方法来实现这一点,请使用这个解决方案=>

var capitalizeString = function (word) {    
    word = word.toLowerCase();
    if (word.indexOf(" ") != -1) { // passed param contains 1 + words
        word = word.replace(/\s/g, "--");
        var result = $.camelCase("-" + word);
        return result.replace(/-/g, " ");
    } else {
    return $.camelCase("-" + word);
    }
}

其他回答

用这个:

String.prototype.toTitleCase = function() { 返回this.charAt(0).toUpperCase() + this.slice(1); } 让STR = 'text'; document.querySelector(#演示)。innerText = str.toTitleCase(); <div class = "app"> <p id = "demo"></p> . < / div >

这个解决方案不使用正则表达式,支持重音字符,而且几乎所有浏览器都支持。

function capitalizeIt(str) {
    if (str && typeof(str) === "string") {
        str = str.split(" ");    
        for (var i = 0, x = str.length; i < x; i++) {
            if (str[i]) {
                str[i] = str[i][0].toUpperCase() + str[i].substr(1);
            }
        }
        return str.join(" ");
    } else {
        return str;
    }
}    

用法:

console.log(大写it ('çao 2nd inside Javascript program '));

输出:

Çao第2内部Javascript程序

使用JavaScript和html

String.prototype.capitalize = function() { 返回this.replace (/ (^ | \ s) ([a - z]) / g函数(m, p1, p2) { return p1 + p2.toUpperCase(); }); }; <form name="form1" method="post"> <input name="instring" type="text" value="this is the text string" size="30"> <input type="button" name="Capitalize" value="Capitalize >>" onclick="form1.outstring.value=form1.instring.value.capitalize();" > <input name="outstring" type="text" value="" size="30"> > < /形式

基本上,你可以用string。capitalize()它会大写每个单词的第一个字母。

来源:http://www.mediacollege.com/internet/javascript/text/case-capitalize.html

还有loctus: https://locutus.io/php/strings/ucwords/,它是这样定义的:

function ucwords(str) {
  //  discuss at: http://locutus.io/php/ucwords/
  // original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
  // improved by: Waldo Malqui Silva (http://waldo.malqui.info)
  // improved by: Robin
  // improved by: Kevin van Zonneveld (http://kvz.io)
  // bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
  // bugfixed by: Cetvertacov Alexandr (https://github.com/cetver)
  //    input by: James (http://www.james-bell.co.uk/)
  //   example 1: ucwords('kevin van  zonneveld')
  //   returns 1: 'Kevin Van  Zonneveld'
  //   example 2: ucwords('HELLO WORLD')
  //   returns 2: 'HELLO WORLD'
  //   example 3: ucwords('у мэри был маленький ягненок и она его очень любила')
  //   returns 3: 'У Мэри Был Маленький Ягненок И Она Его Очень Любила'
  //   example 4: ucwords('τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός')
  //   returns 4: 'Τάχιστη Αλώπηξ Βαφής Ψημένη Γη, Δρασκελίζει Υπέρ Νωθρού Κυνός'

  return (str + '').replace(/^(.)|\s+(.)/g, function ($1) {
    return $1.toUpperCase();
  });
};

既然每个人都给了您所要求的JavaScript答案,我将添加CSS属性text-transform: capitalize将执行此操作。

我意识到这可能不是你想要的-你没有给我们任何上下文,你正在运行这个-但如果它只是为了表示,我肯定会选择CSS替代方案。