我正在尝试使用 RegExp
来匹配短语“/$”构造函数,但无论进行多少转义似乎都无济于事。我错过了什么吗?
RegExp("\/\$").test("/$")
// false
RegExp("/$").test("/$")
// false
RegExp("\/$").test("/$")
// false
RegExp("/\$").test("/$")
// false
最佳答案
您需要使用 \\
而不是 \
并且不需要转义 /
或使用正则表达式 /\/直接\$/
。检查RegExp
documentation了解更多信息。
When using the constructor function, the normal string escape rules (preceding special characters with \ when included in a string) are necessary.
例如,/\w+/
相当于 new RegExp('\\w+')
console.log(
RegExp("/\\$").test("/$")
)
//or
console.log(
/\/\$/.test("/$")
)
关于javascript - 在 RegExp 构造函数中匹配字符串 "/$",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38450206/