我在一个字符串中有用户的名字和姓氏,其间有空格
例如
John Doe
Peter Smithon
现在我希望将此字符串转换为两个字符串 - 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
这适用于双字全名。但如果fullname有一个单词,那么我就有问题
var fullname = "john"
var last = fullname.replace(/^.*\s/, "").toUpperCase().trim(); // john
var first = fullname.replace(/\s.*$/, "").toUpperCase().trim(); // john
答案 0 :(得分:8)
使用split + shift方法。
var parts = "Thomas Mann".split(" "),
first = parts.shift(),
last = parts.shift() || "";
因此,如果是单个单词名称,它将为您提供预期结果:
last = "";
答案 1 :(得分: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/
答案 2 :(得分: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);
答案 3 :(得分:0)
如果在任何情况下你都只是“先到后”,你可以使用:
var string = "john "
var i = string.split(" ");
alert("first: "+i[0]+ "\n"+ "last: " + i[1]);
答案 4 :(得分:0)
var str ='John';
var str2 ='Peter Smithon';
var str3 ='彼得';
var words = str.split( / \ s + / g ,2);
var first = words [0];
var last = words [1] ||'';
警告(第一个+''+最后一个);
答案 5 :(得分:0)
我知道这已经被回复并标记为已回答但我只是想注意,如果您仍然想要使用正则表达式,您可以更改“最后”表达式:
var last = string.replace(/^[a-zA-Z]*/, "").toUpperCase().trim();
答案 6 :(得分: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);
});
});