转义正斜杠 [英] Escape forward slash
问题描述
我正在使用以下代码进行不区分大小写的搜索:
I am using this code for case insensitive searching:
Effect.find({name: new RegExp('^'+name+'$', "i")}).lean().exec(function(err, effect){
if(err) return callback(err, null);
callback(null, effect);
});
乍一看看起来不错.
问题:
我有一个叫test A/c
的效果.
当我在数据库中搜索不区分大小写的精确匹配项时,我得到了更奇怪的结果:
When I search for an exact case insensitive match in the database, I get wiered results:
案例:1 当我搜索test A/
时,我找到了一个匹配项.
Case:1 When I search for test A/
, I get a match found.
案例:2 另外,对于test A/c
,我也找到了匹配项.
Case:2 Also for test A/c
, I get a match found.
哪种情况不适合第一种情况.我知道这是我的Regex表达问题.但是,我对Regex还是陌生的.
Which is not right for the first case. I know that is a problem with my Regex expression. But I am fairly new to Regex.
推荐答案
最后发现了错误. new RegExp()
函数不需要转义backslash
.我的错误是,我是从url发送此值的,因此在斜杠后将其计为新参数.
At last found the mistake. new RegExp()
function does not need to escape backslash
. My mistake was, I was sending this value from url and thus it was counted as new parameter after slash.
这篇关于转义正斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!