8

ユーザーの名と姓を1つの文字列に入れ、たとえば間にスペースを入れます
John Doe
Peter Smithon

そして今、私はこの文字列を2つの文字列に変換したいと思います-firstnameとlastname-
John Doe> first = John、last = Doe
John-> first = John、last = ""
[space]Doe-> first = ""、last= Doe

次のコードを使用しています

var fullname = "john Doe"
var last = fullname.replace(/^.*\s/, "").toUpperCase().trim(); // john
var first = fullname.replace(/\s.*$/, "").toUpperCase().trim(); // Doe  

これは2語のフルネームでうまく機能します。しかし、フルネームに1つの単語がある場合、問題が発生します

var fullname = "john"  
var last = fullname.replace(/^.*\s/, "").toUpperCase().trim(); // john
var first = fullname.replace(/\s.*$/, "").toUpperCase().trim(); // john  

http://jsfiddle.net/YyCKx/
何かアイデアはありますか?

4

7 に答える 7

12

分割 + シフト方式を使用します。

var parts = "Thomas Mann".split(" "),
    first = parts.shift(),
    last = parts.shift() || "";

したがって、単一の単語名の場合、期待される結果が得られます。

last = "";
于 2012-10-24T08:26:25.273 に答える
1

次のコードを使用します。

行を変更する必要があります: splitFullName("firstName","lastName","fullName"); フォームの正しいフィールド ID が含まれていることを確認してください。


function splitFullName(a,b,c){
   String.prototype.capitalize = function(){
   return this.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );
  };
document.getElementById(c).oninput=function(){
    fullName = document.getElementById(c).value;
    if((fullName.match(/ /g) || []).length ===0 || fullName.substring(fullName.indexOf(" ")+1,fullName.length) === ""){
        first = fullName.capitalize();;
        last = "null";
    }else if(fullName.substring(0,fullName.indexOf(" ")).indexOf(".")>-1){
        first = fullName.substring(0,fullName.indexOf(" ")).capitalize() + " " + fullName.substring(fullName.indexOf(" ")+1,fullName.length).substring(0,fullName.substring(fullName.indexOf(" ")+1,fullName.length).indexOf(" ")).capitalize();
        last = fullName.substring(first.length +1,fullName.length).capitalize();
    }else{
        first = fullName.substring(0,fullName.indexOf(" ")).capitalize();
        last = fullName.substring(fullName.indexOf(" ")+1,fullName.length).capitalize();
    }
    document.getElementById(a).value = first;
    document.getElementById(b).value = last;
};
//Initial Values
if(document.getElementById(c).value.length === 0){
    first = document.getElementById(a).value.capitalize();
    last = document.getElementById(b).value.capitalize();
    fullName =  first + " " + last ;
    console.log(fullName);
    document.getElementById(c).value = fullName;}}

     //Replace the ID's below with your form's field ID's
     splitFullName("firstName","lastName","fullName");

ソース: http://developers.marketo.com/blog/add-a-full-name-field-to-a-marketo-form/

于 2014-12-22T16:27:31.927 に答える
0
jQuery( window ).load(function() {
            jQuery("#FullNametest").change(function(){
                var temp = jQuery(this).val();
                var fullname = temp.split(" ");
                var firsname='';
                var middlename='';
                var lastname = '';
                firstname=fullname[0];
                lastname=fullname[fullname.length-1];

                for(var i=1; i < fullname.length-1; i++) 
                {
                   middlename =  middlename +" "+ fullname[i];
                }
                jQuery('#FirstName').val(firstname);
                jQuery('#LastName').val(lastname);
            });
        });
于 2014-06-03T10:54:38.257 に答える
0

これはすでに返信され、回答済みとしてマークされていることは知っていますが、それでも正規表現を使用したい場合は、「最後の」式を変更できることに注意してください。

var last = string.replace(/^[a-zA-Z]*/, "").toUpperCase().trim();
于 2012-10-24T11:07:01.737 に答える
0

splitメソッドを使用できます

    var string = "ad";
var arr = string.split(" ");
var last = arr[0];
var first = arr[1];
if(first == null){
    first = "";
}
alert(last + "\n" + first)​;​
于 2012-10-24T08:28:02.117 に答える
0

var str='ジョン';

var str2='ピーター スミソン';

var str3='ピーター';

var words=str.split( /\s+/g ,2);

var first=words[0];

var last=words[1]||'';

アラート(最初+''+最後);

于 2012-10-24T10:18:45.440 に答える
0

すべての状況で「最初と最後」だけがある場合は、次を使用できます。

       var string = "john "
       var i = string.split(" ");
       alert("first: "+i[0]+ "\n"+ "last: " + i[1]);
于 2012-10-24T08:31:08.467 に答える