I can have one of the following strings:
var username = "i:0#.f|myprovider|domain\\myuser"
var username2 = "myprovider|domain\\myuser"
var username3 = "myuser"
I always want only the myuser
part of the string!
I tried the following in JS
var n = username.lastIndexOf('|');
var domainandusername = username.substring(n + 1);
var m = domainandusername.lastIndexOf('"\\"');
var username = domainandusername.substring(m + 1);
I have tried it within a simple page with JS and it seems to work. But is this a good way to solve this? I mean is it a good practice even to do this for string username2
and username3
where I know that they, for example, do not have a |
and \\
?
4 Answers 4
You don't need to over-complicate things. The myuser
part will always come at the end of the string, after \\
if it's there.
All you need is this:
username.split("\\").pop();
Even if the username is entered like the third version, the myuser
will be returned.
-
\$\begingroup\$ Uh... definitely the more simple and elegant one. We all feel stupid now :) \$\endgroup\$cFreed– cFreed2016年09月13日 09:53:18 +00:00Commented Sep 13, 2016 at 9:53
-
\$\begingroup\$ @cFreed Honestly, I was doubting myself when I put this down because of all the other answers; I thought for sure I was missing something. \$\endgroup\$SirPython– SirPython2016年09月14日 01:10:06 +00:00Commented Sep 14, 2016 at 1:10
sounds like you want to do split
var domainAndUsername = username.split('|').pop()
var username = domainAndUsername.split("\\").pop()
-
\$\begingroup\$ line2 is the correct answer, and
.split("\\").pop()
will work for all 3 types of username format given in the OP \$\endgroup\$Jonah– Jonah2016年09月12日 20:01:39 +00:00Commented Sep 12, 2016 at 20:01
A regex match would be simpler, though it may be overkill.
var strings = [
"i:0#.f|myprovider|domain\\myuser",
"myprovider|domain\\myuser",
"myuser"
];
for (var string of strings) {
console.log(
string, '->',
// here is the only useful line:
string.match(/(?:^|\\)([^\\]*)$/)[1]
);
}
-
\$\begingroup\$ The syntax of your regular expression confuses me : /(?:^|\)([^\]*)$/ Is a positive lookahead not " ?= " instead of " ?: " ? \$\endgroup\$michael.zech– michael.zech2016年09月13日 06:35:26 +00:00Commented Sep 13, 2016 at 6:35
-
\$\begingroup\$ @mizech Not a lookahead, merely a non-capturing flag. Instead I could have chosen
match(/(^|\\)([^\\]*$/)[2]
. \$\endgroup\$cFreed– cFreed2016年09月13日 09:50:50 +00:00Commented Sep 13, 2016 at 9:50 -
\$\begingroup\$ Haven't known it before. Thanks a lot for your hint. Cool. \$\endgroup\$michael.zech– michael.zech2016年09月13日 10:45:04 +00:00Commented Sep 13, 2016 at 10:45
-
\$\begingroup\$ Overkill indeed. \$\endgroup\$Simon Forsberg– Simon Forsberg2016年09月13日 12:20:41 +00:00Commented Sep 13, 2016 at 12:20
var n = username.lastIndexOf('|'); var domainandusername = username.substring(n + 1);
This part of the solution doesn't actually do anything with the given inputs. It would seem that you could delete these lines and only check for the backslash.
If you do need these lines, you may want to add another example showing why.
-
\$\begingroup\$ This I totally agree with. There's no reason to use
.split
or regex, just.substring
and.lastIndexOf
is enough. \$\endgroup\$Simon Forsberg– Simon Forsberg2016年09月13日 12:21:46 +00:00Commented Sep 13, 2016 at 12:21
username
and then avar username
? \$\endgroup\$var username
. The other usernamex are possible variants which must be also processable. \$\endgroup\$