如何在 ruby​​ 中使用 .match 方法转义字符串中的单引号

发布于 2024-12-02 03:53:42 字数 222 浏览 3 评论 0原文

我的代码是这样的:

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 技术交流群。

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

发布评论

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

评论(3

陌上芳菲 2024-12-09 03:53:42

要解决 Xcode 语法荧光笔的缺点,您可以将单引号替换为该字符的八进制转义代码:

/===========(.*?)What\047s New in this Version/m

PS。您还可以稍微缩短正则表达式的开头:

/={11}(.*?)What\047s New in this Version/m

To work around the shortcomings of the Xcode syntax highlighter, you can replace the single quote with the octal escape code for that character:

/===========(.*?)What\047s New in this Version/m

PS. You can also shorten the start of your regex a bit:

/={11}(.*?)What\047s New in this Version/m
明媚如初 2024-12-09 03:53:42

通常使用反斜杠“\”来转义特殊字符:

/===========(.*?)What\'s New in this Version/m

You typically use the backslash "\" to escape special characters:

/===========(.*?)What\'s New in this Version/m
煮茶煮酒煮时光 2024-12-09 03:53:42

这似乎是 XCode 的错误。您可以尝试用以下方法解决它:

/===========(.*?)What.s New in this Version/m
# fix here ----------^ 

It seems to be an XCode bug. You can try and get around it with this:

/===========(.*?)What.s New in this Version/m
# fix here ----------^ 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文