[英]Extracting full name from string using regex javascript
"Name: Roger LeftPhone: (848) 274-9377Email: rogerleft@trueworld.ai" “姓名:Roger Left 电话:(848) 274-9377Email:rogerleft@trueworld.ai”
What is the regex expression (javascript) that I need to extract "Roger Left" from the string above?我需要从上面的字符串中提取“Roger Left”的正则表达式(javascript)是什么?
Thanks!!谢谢!!
You can get the name in group 1 with this regex :您可以使用此正则表达式获取第 1 组中的名称:
Name:\s*(.*?)Phone:
We assume here, that the name is any string which starts immediately after "Name:" and ends at "Phone:".我们在这里假设名称是紧跟在“Name:”之后开始并以“Phone:”结束的任何字符串。 The
\\s*
pattern strips away the leading spaces from the name. \\s*
模式从名称中去除前导空格。 The :
after Phone
is important, because it is possible that someone will have a name including Phone
, for example: Phone
之后的:
很重要,因为可能有人的名字包含Phone
,例如:
Name: Phoney ChanPhone: .....
Without :
after phone we would have matched the empty string in front of Phoney
.如果没有
:
在 phone 之后,我们将匹配Phoney
前面的空字符串。
^Name:\s(.+)Phone:\s(.+)Email:\s(.+)
This works as follows:其工作原理如下:
^Name:
matches everything that starts with Name:
^Name:
匹配以Name:
开头的所有内容Name:
\\s
Matches all white spaces. \\s
匹配所有空格。
(.+)Phone
matches all characters until Phone
(.+)Phone
匹配所有字符直到Phone
With that you can get the name.这样你就可以得到名字了。 The rest is for you to get the rest of the elements in case that is needed.
剩下的就是让您在需要时获取其余元素。
If you just need the name then ^Name:\\s(.+)Phone:
should be enough.如果您只需要名称,那么
^Name:\\s(.+)Phone:
就足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.