Javascript 拆分单词的第一个字符

Javascript 拆分单词的第一个字符,javascript,Javascript,我有一个要求,那就是我必须只取两个单词的第一个字母。就像我从Web服务得到的响应是John Cooper,我必须从中获取JC 我尝试了sbstr(0,2),但这需要JO,有没有任何方法可以像上面那样形成 您可以在web上找到一些现成的好javascript函数: function getInitials(x) { //(x is the name, e.g. John Cooper) //create a new variable 'seperateWords'

我有一个要求,那就是我必须只取两个单词的第一个字母。就像我从Web服务得到的响应是
John Cooper
,我必须从中获取
JC


我尝试了
sbstr(0,2)
,但这需要JO,有没有任何方法可以像上面那样形成

您可以在web上找到一些现成的好javascript函数:

function getInitials(x)
{
        //(x is the name, e.g. John Cooper)

        //create a new variable 'seperateWords'
        //which uses the split function (by removing spaces)
        //to create an array of the words
        var seperateWords = x.split(" ");

        //also create a new empty variable called acronym
        //which will eventually store our acronym
        var acronym = "";

        //then run a for loop which runs once for every
        //element in the array 'seperateWords'.
        //The number of elements in this array are ascertained
        //using the 'seperateWords.length' variable
        for (var i = 0; i < seperateWords.length; i++){

            //Eacy letter is added to the acronym
            //by using the substr command to grab
            //the first letter of each word
            acronym = (acronym + seperateWords[i].substr(0,1));
        }

        //At the end, set the value of the field
        //to the (uppercase) acronym variable
        // you can store them in any var or any HTML element
        document.register.username2.value = toUpperCase(acronym);
}
函数getInitials(x)
{
//(x是名称,例如John Cooper)
//创建新变量“SeparateWords”
//使用拆分功能的(通过删除空格)
//创建一个单词数组
var separatewords=x.split(“”);
//还要创建一个名为acronym的新空变量
//这将最终存储我们的首字母缩略词
var首字母缩写=”;
//然后运行for循环,该循环每次运行一次
//数组“separatewords”中的元素。
//此数组中的元素数已确定
//使用“separateWords.length”变量
for(var i=0;i
您在尝试中错过的技巧是首先
拆分
姓名,以分离名字和姓氏

[]

带和:

与:

var name=“John Cooper”;
var缩写=”;
var wordArray=name.split(“”);

对于(var i=0;i好吧,如果我让你写的话,那么就试试下面的方法

var words = 'John Cooper'.split(' ');
var shortcut = words[0][0] + words[1][0];
alert(shortcut);
//如果你确定那是你的名字的话


注意:)

为了概括@katspaugh给出的正则表达式答案,这将适用于任何字数的所有字符串,无论第一个字母是否大写

'John Cooper workz'.replace(/\W*(\w)\w*/g, '$1').toUpperCase()
将导致
JCW

显然,如果要保留每个单词的第一个字母的大小写,只需删除
toUpperCase()

旁注


使用这种方法,
John McCooper
之类的东西将导致
JM
而不是
JMC

下面是一个正则表达式解决方案,它支持重音字母(如a)和非拉丁语(如希伯来语),并且不假设名称为驼峰大小写:

var name='0ḟ面向对象Ḃar′;
var initials=name.replace(/\s*(\s)\s*/g,$1').toUpperCase();
document.getElementById('output')。innerHTML=首字母缩写

是非标准的,更好地使用或修改。我不知道-已将其更改为
substring()
。此正则表达式假定每个单词都以大写字母开头。其他答案有更好的正则表达式。@Groady错了。这个regexp只是用空字符串替换任何小写字母。在本例中,插入符号字符
^
表示
不是
(因此括号中的组与除大写字母以外的任何字符匹配)。
var name = "John Cooper";
var initials = "";
var wordArray = name.split(" ");
for(var i=0;i<wordArray.length;i++)
{
    initials += wordArray[i].substring(0,1);
}
document.write(initials);
var words = 'John Cooper'.split(' ');
var shortcut = words[0][0] + words[1][0];
alert(shortcut);
'John Cooper workz'.replace(/\W*(\w)\w*/g, '$1').toUpperCase()