我无法理解下面的代码:
var reg=/|/;
reg.test('ssdws');//returns true no matter what the string parameter is.
Run Code Online (Sandbox Code Playgroud)
我知道结果,但为什么?
/|/意味着<empty string>OR <empty string>,实际意味着一种模式<empty string>.
由于在2个字符之间存在无限多个空字符串,因此在第一个字符之前,在最后一个字符之后或在空字符串中,/|/.test(input)将返回true任何字符串.
如果你想匹配文字字符|,你需要逃避它,即/\|/.然后/\|/.test(input)将返回true 当且仅当有一个|输入字符串.
| 归档时间: |
|
| 查看次数: |
84 次 |
| 最近记录: |