0

I have been browsing lots of solutions, but somewhy haven't got anything to work.

I need to replace following string: "i:0#.w|dev\\tauri;" with "i:0#.w|dev\tauri;"

I have tried following JS codes to replace:

s.replace(/\\\\/g, "\\$1");

s.replace(/\\\\/g, "\\");

But have had no result. Yet following replaced my \\ with "

s.replace(/\\/g, "\"");

To be honset, then I am really confused behind this logic, it seems like there should be used \\\\ for double backshashed yet it seems to work with just \\ for two backshashes..

I need to do this for comparing if current Sharepoint user (i:0#.w|dev\tauri) is on the list.

Update:

Okay, after I used console.log();, I discovered something interesting.

Incode: var CurrentUser = "i:0#.w|dev\tauri"; and console.log(): i:0#.w|dev auri...

C# code is following:

SPWeb theSite = SPControl.GetContextWeb(Context);
SPUser theUser = theSite.CurrentUser;
return theUser.LoginName;
Taurib
  • 451
  • 9
  • 26
  • 1
    If the first two did nothing, and `s.replace(/\\/g, "\"");` replaced your `\\ ` with a single `"`, then you only had one backslash in the string to begin with. Don't confuse what you see in your debugger with the actual number of backslashes in the string. – JLRishe Jan 03 '15 at 18:21
  • possible duplicate of [Replace double backslashes with a single backslash in javascript](http://stackoverflow.com/questions/25304214/replace-double-backslashes-with-a-single-backslash-in-javascript) See also: [replace “\\” with “\” in a string in C#](http://stackoverflow.com/questions/7482360/replace-with-in-a-string-in-c-sharp) – JLRishe Jan 03 '15 at 18:22
  • But the problem is that, even if I don't try to replace, then comparing two strings is still false – Taurib Jan 03 '15 at 19:38
  • 1
    Do the semicolons have anything to do with it, or are those a typo? Please do this: do a `console.log()` with the string you are trying to modify, and do a `console.log()` with the strings you are matching it against, and observe whether there is any difference between them. – JLRishe Jan 03 '15 at 19:40
  • Check out my answer. That's because \t is the escape code for a tab. – peterjb Jan 03 '15 at 22:41

1 Answers1

1

JavaScript strings need to be escaped so if you are getting a string literal with two back slashes, JavaScript interprets it as just one. In your string you are using to compare, you have \t, which is a tab character, when what you probably want is \\t. My guess is that wherever you are getting the current SharePoint user from, it is being properly escaped, but your compare list isn't.

Edit:

Or maybe the other way around. If you're using .NET 4+ JavaScriptStringEncode might be helpful. If you're still having problems it might help to show us how you are doing the comparison.

peterjb
  • 819
  • 7
  • 6