javascript正则表达式,拆分用户';她的全名
我将用户的名字和姓氏放在一个字符串中,中间有空格 e、 g.javascript正则表达式,拆分用户';她的全名,javascript,regex,Javascript,Regex,我将用户的名字和姓氏放在一个字符串中,中间有空格 e、 g. johndoe Peter Smithon 现在我想把这个字符串转换成两个字符串-firstname和lastname johndoe->第一个=John,最后一个=Doe John->first=John,last=“” [space]Doe->first=”“,last=Doe 我正在使用下一个代码 var fullname = "john Doe" var last = fullname.replace(/^.*\s/, "")
johndoe
Peter Smithon
现在我想把这个字符串转换成两个字符串-firstname和lastnamejohndoe
->第一个=John
,最后一个=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
有什么想法吗?使用拆分+移位的方法
var parts = "Thomas Mann".split(" "),
first = parts.shift(),
last = parts.shift() || "";
因此,对于单个单词名称,它将为您提供预期结果:
last = "";
您可以使用
split
方法
var string = "ad";
var arr = string.split(" ");
var last = arr[0];
var first = arr[1];
if(first == null){
first = "";
}
alert(last + "\n" + first);
如果在每种情况下您都只有“第一个-最后一个”,您可以使用:
var string = "john "
var i = string.split(" ");
alert("first: "+i[0]+ "\n"+ "last: " + i[1]);
var str='John'
var str2='Peter Smithon'
var str3='Peter'
var words=str.split(/\s+/g,2)
var first=单词[0]
var last=单词[1]| |“
警报(第一次+“”+最后一次) 我知道这已经被回复并标记为已回复,但我只想指出,如果您仍然想使用regex,您可以更改“last”表达式:
var last = string.replace(/^[a-zA-Z]*/, "").toUpperCase().trim();
jQuery(窗口).load(函数(){
jQuery(“#FullNametest”).change(函数(){
var temp=jQuery(this.val();
var fullname=临时拆分(“”);
var firsname=“”;
var middlename='';
var lastname='';
firstname=全名[0];
lastname=fullname[fullname.length-1];
对于(变量i=1;i
使用以下代码:
您需要更改行:splitFullName(“firstName”、“lastName”、“fullName”);并确保它包含表单中正确的字段ID
来源:您永远无法准确拆分名称字符串。您永远无法知道有多少名或姓,或者用户键入它们的顺序。如果我有一个名字叫“范东英”或“利托·德拉塞尔纳”,你怎么知道哪个是名字和姓氏?最好是使用单独的输入框。我将对该答案的编辑回滚,因为编辑该答案的人在将其转换为代码格式时将
**
标记保留为粗体。按照目前的标准,这个答案缺少一个解释,但考虑到2012年的答案可能不适用于同样的方式。
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);
});
});
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");