Javascript用单反斜杠替换双反斜杠 [英] Javascript replacing double backslashed with single backslash

查看:176
本文介绍了Javascript用单反斜杠替换双反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了很多解决方案,但为什么没有任何可用的方法.

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

我需要替换以下字符串:"i:0#.w|dev\\tauri;""i:0#.w|dev\tauri;"代码>

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

我尝试过以下 JS 代码来替换:

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..

我需要这样做以比较当前 Sharepoint 用户 (i:0#.w|dev\tauri) 是否在列表中.

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

更新:

好的,在我使用了 console.log(); 之后,我发现了一些有趣的东西.

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

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

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

C# 代码如下:

SPWeb theSite = SPControl.GetContextWeb(Context);
SPUser theUser = theSite.CurrentUser;
return theUser.LoginName;

推荐答案

JavaScript 字符串需要转义 所以如果你得到一个带有两个反斜杠的字符串文字,JavaScript 会将它解释为一个.在您用来比较的字符串中,您有 \t,它是一个制表符,而您可能想要的是 \\t.我的猜测是,无论您从何处获取当前 SharePoint 用户,它都会被正确转义,但您的比较列表却不是.

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.

或者反过来.如果您使用 .NET 4+ JavaScriptStringEncode 可能会有所帮助.如果您仍然遇到问题,向我们展示您是如何进行比较可能会有所帮助.

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.

这篇关于Javascript用单反斜杠替换双反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆