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;
5
  • 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. Commented Jan 3, 2015 at 18:21
  • possible duplicate of Replace double backslashes with a single backslash in javascript See also: replace “\\” with “\” in a string in C# Commented Jan 3, 2015 at 18:22
  • But the problem is that, even if I don't try to replace, then comparing two strings is still false Commented Jan 3, 2015 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. Commented Jan 3, 2015 at 19:40
  • Check out my answer. That's because \t is the escape code for a tab. Commented Jan 3, 2015 at 22:41

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Got it work in code behind with Regex.Escape();, thank you! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.