javascript正则表达式,分割用户的全名

问题描述 投票:5回答:7

我在一个字符串中包含用户的名字和姓氏,中间有空格例如John DoePeter Smithon

现在我想将此字符串转换为两个字符串-名和姓John Doe->第一个= John,最后一个=DoeJohn->第一个= John,最后一个=“”[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/有什么想法吗?

javascript regex
7个回答
9
投票

使用拆分+移位方法。

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

因此,如果使用单个单词名称,它将为您提供预期的结果:

last = "";

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/


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

0
投票

如果在每种情况下您都只能使用“倒数第一”:

       var string = "john "
       var i = string.split(" ");
       alert("first: "+i[0]+ "\n"+ "last: " + i[1]);

0
投票

[我知道这已经被答复并标记为已回答,但是我只想指出,如果您仍然想使用正则表达式,则可以更改“ last”表达式:

var last = string.replace(/^[a-zA-Z]*/, "").toUpperCase().trim();

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

0
投票
var str='John';

var str2='Peter Smithon';

var str3='Peter';

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

var first=words[0];

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

alert(first+' '+last);
推荐问答
热门问答
最新问答