我要像做Rails的 - 你怎么插入一个变量为正则表达式(正则表达式)例如assert_match
assert_match /blah blah blah #{@user}/, @some_text
,但我并没有使用它的任何运气。
我在这里做错了什么?
我要像做Rails的 - 你怎么插入一个变量为正则表达式(正则表达式)例如assert_match
assert_match /blah blah blah #{@user}/, @some_text
,但我并没有使用它的任何运气。
我在这里做错了什么?
这是插入一个变量为正则表达式的正确方法:
irb(main):001:0> a='Hi'
=> "Hi"
irb(main):002:0> b=/Not #{a}/
=> /Not Hi/
所以你的问题很可能是断言,是因为一个糟糕的比赛的失败。检查@user和@some_text的价值,并尝试http://rubular.com拿出匹配的正则表达式
regxPlayerVariable =” = * ?document \ .getElementById(#{pluginPlayeVariable})'在这个我应用以上所有,但这些都不工作 – SSP 2014-08-21 07:15:09
assert_match还需要一个字符串,如
assert_match("blah blah", 'a string')
因此其他的方式做你正在做的事情。将用户串插
string_to_match = "blah blah #{@user}"
assert_match(string_to_match, 'a string')
如果有包含特殊字符的正则表达式的字符串@user的可能性,您应该修改这:
/blah blah blah #{Regexp.escape(@user)}/
你可以看一看%R {},例如:(。*?)。
pattern = 'foo'
%r{#{pattern}_bar} =~ 'foo_bar_2000'
为德鲁斯说,这已经是这样 – knoopx 2009-12-17 14:56:39