Javascript Regular Expression尝试将名称拆分为Title / First Name(s)/ Last Name [英] Javascript Regular Expression to attempt to split name into Title/First Name(s)/Last Name
问题描述
我想尝试在Javascript中检测一个人姓名的不同部分,并将其删除,以便我可以将它们传递给其他人。
I want to try and detect the different parts of a person's name in Javascript, and cut them out so that I can pass them onto something else.
姓名可以以任何格式出现 - 例如: -
Names can appear in any format - for example:-
miss victoria mj laing
miss victoria m j laing
- 维多利亚小姐CJ Long
- Bob Smith
- Fred
- Mr Davis
- Miss Victoria C J Long
- Bob Smith
- Fred
- Mr Davis
我想尝试写一些简单的东西,这样做是最好的猜测这些并在80%的时间左右得到它们(我们有一些非常狡猾数据)
I want to try and write something simple, that'll do it's best to guess these and get them right 80% of the time or so (We have some extremely dodgy data)
我正在考虑使用正则表达式检查它是否有前缀,然后分支到两个位置,看它是否有
I'm thinking of something along the lines of using a regex to check whether it has a prefix, then branch off to two places as to whether it has
/^(Dr|Mr|Mrs|Miss|Master|etc).? /
然后使用
/(\w+ )+(\w+)/
匹配姓氏和其他名称。虽然,我不确定我在这里的贪婪/不合理的选择,以及我是否可以做一些快捷方式来获得可能有的所有不同路径。基本上,希望找到一些简单的东西,这可以很好地完成工作。
To match last name and other names. Though, I'm unsure on my greedy/ungreedy options here, and whether I can do soemthing to shortcut having all the different paths that might be available. Basically, hoping to find something simple, that does the job in a nice way.
由于ETL工具的局限性,它也必须用Javascript编写。我正在使用。
It's also got to be written in Javascript, due to the limitations of the ETL tool I'm using.
推荐答案
为什么不 split() 并检查结果部分:
Why not split() and just check the resulting parts:
// Split on each space character
var name = "Miss Victoria C J Long".split(" ");
// Check the first part for a title/prefix
if (/^(?:Dr|Mr|Mrs|Miss|Master|etc)\.?$/.test(name[0])) {
name.shift();
}
// Now you can access each part of the name as an array
console.log(name);
//-> Victoria,C,J,Long
工作演示: http://jsfiddle.net/AndyE/p9ra4/
当然,这不会解决人们在评论中提到的其他问题,但是你只需要一个正则表达式就可以解决这些问题。
Of course, this won't work around those other issues people have mentioned in the comments, but you'd struggle on those issues even more with a single regex.
这篇关于Javascript Regular Expression尝试将名称拆分为Title / First Name(s)/ Last Name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!