匹配并替换字符串中所有出现的反斜杠
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RegExp.compile
相当于 JS 中的new Regexp
,因此参数不是正则表达式文字。您的代码应为RegExp.compile("\\", "g")
。但对于这种特殊情况,
name.replace("\\", "\\\\")
应该足够了。RegExp.compile
is the equivalent tonew Regexp
in JS, so the argument is not a regexp literal. Your code should readRegExp.compile("\\", "g")
.But for this particular case,
name.replace("\\", "\\\\")
should be enough.