I'm trying to pull the name "Dave" (but it will be different depending on who is logged in) out of the following string:
我想把“Dave”这个名字从下面的字符串中拉出来(但这取决于谁登录了):
Logged In: Dave - example.com
登录:Dave - example.com。
Any thoughts on the best way to do this with regex in JS?
对于在JS中使用regex的最佳方式有什么想法吗?
3 个解决方案
#1
2
I don't know how you have things exactly, setup, but this should work:
我不知道你是怎么做到的,设置,但这应该是可行的:
// This is our RegEx pattern.
var user_pattern = /Logged In: ([a-z]+) - example\.com/i
// This is the logged in string we'll be matching the pattern against
var logged_in_string = "Logged In: Dave - example.com"
// Now we attempt the actual match. If successful, user[1] will be the user's name.
var user = logged_in_string.match(user_pattern)
My example is lazy and only matches single names that contain letters between a-z because I wasn't sure of your parameters for the username. You can look up additional RegEx patterns depending on your needs.
我的示例是惰性的,只匹配包含a-z之间字母的单个名称,因为我不确定用户名的参数。您可以根据需要查找其他RegEx模式。
Hope this helps!
希望这可以帮助!
#2
2
You don't really need a regex. You could take a .slice()
of the string, getting the position of the :
and the -
using .indexOf()
.
你真的不需要正则表达式。您可以使用字符串的.slice()来获得:和使用. indexof()的位置。
Example: http://jsfiddle.net/mkUzv/1/
例如:http://jsfiddle.net/mkUzv/1/
var str = "Logged In: Dave - example.com";
var name = str.slice( str.indexOf(': ') + 2, str.indexOf(' -') );
EDIT: As noted by @cHao, + 2
should have been used in order to eliminate the space after the :
. Fixed.
编辑:如@cHao所言,应该使用+ 2来消除:后的空格。固定的。
#3
-1
split at the colon, split at the spaces, and take the second item in that array, like:
在冒号处分割,在空格处分割,取该数组中的第二个项,如:
var thestring = "Logged In: Dave - example.com";
thestring = thestring.split(":");
thestring = thestring[1].split(" ");
thename = thestring[1]
Or, if names could contain spaces:
或者,如果名称可以包含空格:
var thestring = "Logged In: Dave - example.com";
thestring = thestring.split(":");
thestring = thestring[1].split("-");
var x = thestring[0].length;
if (x > 4) {
var thename = thestring[0].replace(" ", "");
}
else {
thestring = thestring[0].split(" ");
thestring = thestring.split(" ");
thename = thestring[1] + " " + thestring[3];
}
#1
2
I don't know how you have things exactly, setup, but this should work:
我不知道你是怎么做到的,设置,但这应该是可行的:
// This is our RegEx pattern.
var user_pattern = /Logged In: ([a-z]+) - example\.com/i
// This is the logged in string we'll be matching the pattern against
var logged_in_string = "Logged In: Dave - example.com"
// Now we attempt the actual match. If successful, user[1] will be the user's name.
var user = logged_in_string.match(user_pattern)
My example is lazy and only matches single names that contain letters between a-z because I wasn't sure of your parameters for the username. You can look up additional RegEx patterns depending on your needs.
我的示例是惰性的,只匹配包含a-z之间字母的单个名称,因为我不确定用户名的参数。您可以根据需要查找其他RegEx模式。
Hope this helps!
希望这可以帮助!
#2
2
You don't really need a regex. You could take a .slice()
of the string, getting the position of the :
and the -
using .indexOf()
.
你真的不需要正则表达式。您可以使用字符串的.slice()来获得:和使用. indexof()的位置。
Example: http://jsfiddle.net/mkUzv/1/
例如:http://jsfiddle.net/mkUzv/1/
var str = "Logged In: Dave - example.com";
var name = str.slice( str.indexOf(': ') + 2, str.indexOf(' -') );
EDIT: As noted by @cHao, + 2
should have been used in order to eliminate the space after the :
. Fixed.
编辑:如@cHao所言,应该使用+ 2来消除:后的空格。固定的。
#3
-1
split at the colon, split at the spaces, and take the second item in that array, like:
在冒号处分割,在空格处分割,取该数组中的第二个项,如:
var thestring = "Logged In: Dave - example.com";
thestring = thestring.split(":");
thestring = thestring[1].split(" ");
thename = thestring[1]
Or, if names could contain spaces:
或者,如果名称可以包含空格:
var thestring = "Logged In: Dave - example.com";
thestring = thestring.split(":");
thestring = thestring[1].split("-");
var x = thestring[0].length;
if (x > 4) {
var thename = thestring[0].replace(" ", "");
}
else {
thestring = thestring[0].split(" ");
thestring = thestring.split(" ");
thename = thestring[1] + " " + thestring[3];
}