3

我正在尝试在速度模板中对用户代理进行匹配。

$ua 确实打印出来但匹配!才不是。我究竟做错了什么?

#set( $ua = $request.getHeader('User-Agent'))
<p>$ua</p>
#if( $ua.matches('/Windows.(NT|XP|ME|9)/')) 
<p>Matches!</p>
#end
4

1 回答 1

4

I know this is old, but the solution to this problem is that the regex provided to matches must match the ENTIRE string in order to return true. So, for example:

$ua.matches('Windows.(NT|XP|ME|9)')

returns false, but

$ua.matches('.*Windows.(NT|XP|ME|9).*')

will behave as you expect, returning true if Windows.(...) is in the string.

This is a bit odd and really stymied me for a while today.

PS - no slashes are required in the regex!

于 2013-03-15T19:28:35.777 回答