8

我在一个字符串中有用户的名字和姓氏,例如
John Doe
Peter Smithon

现在我想将此字符串转换为两个字符串 - 名字和姓氏
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  

这适用于两个词的全名。但是如果全名只有一个字,那我就有问题了

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

使用 split + shift 方法。

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

你可以使用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 string = "john "
       var i = string.split(" ");
       alert("first: "+i[0]+ "\n"+ "last: " + i[1]);
于 2012-10-24T08:31:08.467 回答
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 last = string.replace(/^[a-zA-Z]*/, "").toUpperCase().trim();
于 2012-10-24T11:07:01.737 回答
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 回答