匹配并替换字符串中所有出现的反斜杠

发布于 2024-12-08 11:45:07 字数 2476 浏览 0 评论 0原文

我在 GWT 中使用以下命令从字符串中查找反斜杠并替换为 \\。

String name = "\path\item";
name = RegExp.compile("/\\/g").replace(name, "\\\\");

但它不起作用,因为对于 name=\path\item 返回 name=\path\item。

好的,我遵循 Thomas Broyer 的建议,第一个 RegExp.compile("\\", "g").replace(bgPath, "\\\\") 给出:

Caused by: com.google.gwt.core.client.JavaScriptException: (SyntaxError): trailing \ in regular expression
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:237)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:132)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.regexp.shared.RegExp$.compile(RegExp.java)
    at com.ait.gwt.authtool.client.ui.TicketViewer.<init>(TicketViewer.java:197)
    at com.ait.gwt.authtool.client.AuthTool.onViewTicketBtnClicked(AuthTool.java:1942)
    at com.ait.gwt.authtool.client.AuthTool.onMessageReceived(AuthTool.java:1995)
    at com.ait.gwt.authtool.client.events.MessageReceivedEvent.dispatch(MessageReceivedEvent.java:44)
    at com.ait.gwt.authtool.client.events.MessageReceivedEvent.dispatch(MessageReceivedEvent.java:1)
    at com.google.gwt.event.shared.GwtEvent.dispatch(GwtEvent.java:1)
    at com.google.web.bindery.event.shared.SimpleEventBus.doFire(SimpleEventBus.java:193)
    at com.google.web.bindery.event.shared.SimpleEventBus.fireEvent(SimpleEventBus.java:88)
    at com.google.gwt.event.shared.SimpleEventBus.fireEvent(SimpleEventBus.java:52)

第二个 >bgPath.replaceAll("\\", "\\\\") 给出:

Caused by: java.util.regex.PatternSyntaxException: Unexpected internal error near index 1 
\ 
 ^
    at java.util.regex.Pattern.error(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.lang.String.replaceAll(Unknown Source)
    at com.ait.gwt.authtool.client.ui.TicketViewer.<init>(TicketViewer.java:198)

但是当我输入: bgPath = bgPath.replaceAll(Pattern.quote("\"), Matcher.quoteReplacement("\\"));

正常工作(!!),因为它给出: [INFO] [gwt_app] - !!! bgPath=背景\\卡通\\图像

I use the following in GWT to find the backslash from a string and replace with \\.

String name = "\path\item";
name = RegExp.compile("/\\/g").replace(name, "\\\\");

But it does not work, because for name=\path\item returns name=\path\item.

ok i follow the recomendation of Thomas Broyer and the first RegExp.compile("\\", "g").replace(bgPath, "\\\\") gives:

Caused by: com.google.gwt.core.client.JavaScriptException: (SyntaxError): trailing \ in regular expression
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:237)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:132)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.regexp.shared.RegExp$.compile(RegExp.java)
    at com.ait.gwt.authtool.client.ui.TicketViewer.<init>(TicketViewer.java:197)
    at com.ait.gwt.authtool.client.AuthTool.onViewTicketBtnClicked(AuthTool.java:1942)
    at com.ait.gwt.authtool.client.AuthTool.onMessageReceived(AuthTool.java:1995)
    at com.ait.gwt.authtool.client.events.MessageReceivedEvent.dispatch(MessageReceivedEvent.java:44)
    at com.ait.gwt.authtool.client.events.MessageReceivedEvent.dispatch(MessageReceivedEvent.java:1)
    at com.google.gwt.event.shared.GwtEvent.dispatch(GwtEvent.java:1)
    at com.google.web.bindery.event.shared.SimpleEventBus.doFire(SimpleEventBus.java:193)
    at com.google.web.bindery.event.shared.SimpleEventBus.fireEvent(SimpleEventBus.java:88)
    at com.google.gwt.event.shared.SimpleEventBus.fireEvent(SimpleEventBus.java:52)

and the second bgPath.replaceAll("\\", "\\\\") gives:

Caused by: java.util.regex.PatternSyntaxException: Unexpected internal error near index 1 
\ 
 ^
    at java.util.regex.Pattern.error(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.lang.String.replaceAll(Unknown Source)
    at com.ait.gwt.authtool.client.ui.TicketViewer.<init>(TicketViewer.java:198)

but when i type: bgPath = bgPath.replaceAll(Pattern.quote("\"), Matcher.quoteReplacement("\\"));

works normally(!!) as it gives: [INFO] [gwt_app] - !!! bgPath=Background\\Cartoon\\image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何其悲哀 2024-12-15 11:45:07

RegExp.compile 相当于 JS 中的 new Regexp,因此参数不是正则表达式文字。您的代码应为 RegExp.compile("\\", "g")

但对于这种特殊情况,name.replace("\\", "\\\\") 应该足够了。

RegExp.compile is the equivalent to new Regexp in JS, so the argument is not a regexp literal. Your code should read RegExp.compile("\\", "g").

But for this particular case, name.replace("\\", "\\\\") should be enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文