2011-11-15 14 views
15

私は2単語だけの最初の文字を取らなければならないという要件があります。私はJohn CooperとしてWebServiceからの応答を取得し、私はこれからJCを取らなければならないように。単語の最初の文字を分割する

私はsbstr(0,2)を試しましたが、これはJOをとります。上記のような形になります。

答えて

34

'John Cooper'.replace(/[^A-Z]/g, ''); 
+3

本当にそれをして愛します正規表現:)素敵な仕事+1 – Marwan

+1

この正規表現は、各単語が大文字で始まることを前提としています。他の答えは良い正規表現を持っています。 – Groady

+0

@Groadyが間違っています。この正規表現は、すべての小文字を空の文字列で置き換えます。この場合のキャレット文字 '^'は 'not'を意味します(大括弧内のグループは大文字以外のものと一致します)。 – katspaugh

3

あなたはすぐにウェブ上でいくつかの良い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); 
} 

トリックは最初splitに名と姓を分離するための名前です。 regular expressions

'John Cooper'.split(' ').map(function (s) { return s.charAt(0); }).join(''); 

splitmap

[Source]

2
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); 

は、基本的には、スペースで文字列を分割しての最初の文字を取ります各単語。私はあなたは必ずその名前が2つのワード

よろしく:)

+1

['substr'](https://developer.mozilla。org/en/JavaScript/Reference/Global_Objects/String/substr)が標準ではないため、['substring'](https://developer.mozilla.org/ja/JavaScript/Reference/Global_Objects/String/substring)を使用することをお勧めします。 ['slice'](https://developer.mozilla.org/ja/JavaScript/Reference/Global_Objects/String/slice)。 – katspaugh

+0

私はそれを知らなかった - それを 'substring()'に変更しました。 –

3

正規表現は@katspaughによって与えられます。これは、最初の文字が大文字であるかどうかに関係なく、任意の数の単語を含むすべての文字列に対して機能します。あなたはこのアプローチでtoUpperCase()

SIDE注

を削除し、各単語の最初の文字の大文字と小文字を維持したい場合は

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

は明らかにJCW

になり、 John McCooperのようなものはJMではありませんJMC

11

が一般化する中であればそう簡単に次

var words = 'John Cooper'.split(' '); 
var shortcut = words[0][0] + words[1][0]; 
alert(shortcut); 

//ザッツを試し書きましたまあ場合

0

ここでは、ヘブライ語のようにAと非ラテン語のようなアクセント付き文字をサポートする正規表現のソリューションだし、名前はキャメルケースで想定していません:

var name = 'ḟoo Ḃar'; 
 
var initials = name.replace(/\s*(\S)\S*/g, '$1').toUpperCase(); 
 
document.getElementById('output').innerHTML = initials;
<div id="output"></div>

関連する問題