如何在 ruby 中使用 .match 方法转义字符串中的单引号
我的代码是这样的:
description = contents.match(/===========(.*?)What's New in this Version/m)[1].strip
代码运行良好,但现在单引号后面的所有内容都是蓝色的,我需要一个单引号来结束它。但我该把它放在哪里或者我该如何逃避它呢?我尝试在单引号前加一个反斜杠,但这并没有改变任何东西。
My code is this:
description = contents.match(/===========(.*?)What's New in this Version/m)[1].strip
The code runs fine but now everything after the single quote is in blue and I need a single quote to end it. But where would I put it or how would I escape it? I tried putting a backslash before the single quote but that doesn't change anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要解决 Xcode 语法荧光笔的缺点,您可以将单引号替换为该字符的八进制转义代码:
PS。您还可以稍微缩短正则表达式的开头:
To work around the shortcomings of the Xcode syntax highlighter, you can replace the single quote with the octal escape code for that character:
PS. You can also shorten the start of your regex a bit:
通常使用反斜杠“\”来转义特殊字符:
You typically use the backslash "\" to escape special characters:
这似乎是 XCode 的错误。您可以尝试用以下方法解决它:
It seems to be an XCode bug. You can try and get around it with this: