1
\$\begingroup\$

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 \\?

\$\endgroup\$
  • \$\begingroup\$ Why do you have first one username and then a var username? \$\endgroup\$ – Simon Forsberg Sep 13 '16 at 12:23
  • \$\begingroup\$ This is a sample which based on var username. The other usernamex are possible variants which must be also processable. \$\endgroup\$ – STORM Sep 13 '16 at 12:25
4
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Uh... definitely the more simple and elegant one. We all feel stupid now :) \$\endgroup\$ – cFreed Sep 13 '16 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 Sep 14 '16 at 1:10
3
\$\begingroup\$

sounds like you want to do split

var domainAndUsername = username.split('|').pop()
var username = domainAndUsername.split("\\").pop()
\$\endgroup\$
  • \$\begingroup\$ line2 is the correct answer, and .split("\\").pop() will work for all 3 types of username format given in the OP \$\endgroup\$ – Jonah Sep 12 '16 at 20:01
1
\$\begingroup\$

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]
  );
}

\$\endgroup\$
  • \$\begingroup\$ The syntax of your regular expression confuses me : /(?:^|\)([^\]*)$/ Is a positive lookahead not " ?= " instead of " ?: " ? \$\endgroup\$ – michael.zech Sep 13 '16 at 6:35
  • \$\begingroup\$ @mizech Not a lookahead, merely a non-capturing flag. Instead I could have chosen match(/(^|\\)([^\\]*$/)[2]. \$\endgroup\$ – cFreed Sep 13 '16 at 9:50
  • \$\begingroup\$ Haven't known it before. Thanks a lot for your hint. Cool. \$\endgroup\$ – michael.zech Sep 13 '16 at 10:45
  • \$\begingroup\$ Overkill indeed. \$\endgroup\$ – Simon Forsberg Sep 13 '16 at 12:20
1
\$\begingroup\$
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.

\$\endgroup\$
  • \$\begingroup\$ This I totally agree with. There's no reason to use .split or regex, just .substring and .lastIndexOf is enough. \$\endgroup\$ – Simon Forsberg Sep 13 '16 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.