我在使用的铁路网站上工作 辐射CM 并根据第一种方法来建立状态导航 这个链接.

我正在匹配正则表达式上的URL,以确定是否显示每个导航链接的活动状态。例如,这里有两个示例导航元素,一个用于辐射URL /communications/ 还有一个 /communications/press_releases/:

    <r:if_url matches="/communications\/$/"><li class="bottom-border selected">Communications</li></r:if_url>
    <r:unless_url matches="/communications\/$/"><li class="bottom-border"><a href="/communications">Communications</a></li></r:unless_url>
    <r:if_url matches="/communications\/press_releases/"><li class="bottom-border selected">Press Releases</li></r:if_url>
    <r:unless_url matches="/communications\/press_releases/"><li class="bottom-border"><a href="/communications/press_releases">Press Releases</a></li></r:unless_url>

一切都在新闻稿页面上正常工作 - 也就是说,当URL为 /communications/press_releases 新闻稿释放NAV项目适当地获取“选定”类,并且通信NAV项目未选择。但是,通信正则表达式似乎无法正常运行,就像URL是什么时候 /communications/ 两个元素都不具有“选定”类(因此,正则是不匹配的)。但是,我已经测试了

>> "/communications/".match(/communications\/$/)
=> #<MatchData:0x333a4>

在IRB中,正如您所看到的,正则表达式似乎正常。可能是什么原因造成的?

tl; dr: "/communications/" 火柴 /communications\/$/ 在红宝石外壳中,但不在辐射导航的上下文中。这里发生了什么?

有帮助吗?

解决方案

Radiant的Wiki, ,看来您不需要添加 /S周围的正则逃脱 /s。尝试:

<r:if_url matches="/communications/$"><li class="bottom-border selected">Communications</li></r:if_url>
<r:unless_url matches="/communications/$"><li class="bottom-border"><a href="/communications">Communications</a></li></r:unless_url>
<r:if_url matches="/communications/press_releases/"><li class="bottom-border selected">Press Releases</li></r:if_url>
<r:unless_url matches="/communications/press_releases/"><li class="bottom-border"><a href="/communications/press_releases">Press Releases</a></li></r:unless_url>

怎么了 幕后 那是辐射的电话吗 Regex.new 在字符串上 matches, ,所以您之前试图匹配的正则是:

 Regexp.new '/communications\/$/'
# => /\/communications\/$\// 

这转化为“ Slash Communications Slash of-Offine Slash”,我真的怀疑您想要的。

Ruby Regexs很有趣,因为有两个开始的符号(^)和线的结束($)和开始(\A)和字符串的结尾(\Z)。这就是为什么有时您会看到人们在使用 \A\Z 在他们的言论中。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top