Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
    
Why do you have first one username and then a var username? – Simon Forsberg 2 days ago
    
This is a sample which based on var username. The other usernamex are possible variants which must be also processable. – STORM 2 days ago
up vote 4 down vote accepted

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.

share|improve this answer
    
Uh... definitely the more simple and elegant one. We all feel stupid now :) – cFreed 2 days ago
    
@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. – SirPython yesterday

sounds like you want to do split

var domainAndUsername = username.split('|').pop()
var username = domainAndUsername.split("\\").pop()
share|improve this answer
    
line2 is the correct answer, and .split("\\").pop() will work for all 3 types of username format given in the OP – Jonah 2 days ago

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

share|improve this answer
    
The syntax of your regular expression confuses me : /(?:^|\)([^\]*)$/ Is a positive lookahead not " ?= " instead of " ?: " ? – mizech 2 days ago
    
@mizech Not a lookahead, merely a non-capturing flag. Instead I could have chosen match(/(^|\\)([^\\]*$/)[2]. – cFreed 2 days ago
    
Haven't known it before. Thanks a lot for your hint. Cool. – mizech 2 days ago
    
Overkill indeed. – Simon Forsberg 2 days ago
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.

share|improve this answer
    
This I totally agree with. There's no reason to use .split or regex, just .substring and .lastIndexOf is enough. – Simon Forsberg 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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