Javascript模式不匹配

Javascript模式不匹配,javascript,Javascript,我正在尝试匹配包含管道(|)操作符的模式 已使用以下代码匹配patteren var format = /[ \\|]/; // This is the pattern for matching pipe pattern if ("Near raghavendra temple ring roads".match(format)) { alert("Invalid character"); } “靠近raghavendra temple环形道路”此字符串不包含|运算符,但仍高于条件

我正在尝试匹配包含管道(|)操作符的模式

已使用以下代码匹配patteren

var format = /[ \\|]/; // This is the pattern for matching pipe pattern

if ("Near raghavendra temple ring roads".match(format)) {
    alert("Invalid character");
} 
“靠近raghavendra temple环形道路”
此字符串不包含|运算符,但仍高于条件变为真

无法理解上述模式中的错误。

var-format=/[\\\\\\]/;//这是用于匹配管道阵列的阵列
if(“拉格哈文德拉神庙环路附近”。匹配(格式))
console.log(“无效字符”);
其他的
console.log('All valid')
var-format=/[\\\\\\]/;//这是用于匹配管道阵列的阵列
if(“拉格哈文德拉神庙环路附近”。匹配(格式))
console.log(“无效字符”);
其他的

console.log('All valid')问题是您在其中有一个空间:

/[ \\|]/
实际上意思是“匹配任何一个空格,
\
\

如果您只想匹配管道,请使用以下方法:

const format=/[\\\124;]/;//这是用于匹配管道阵列的阵列
if(“拉格哈文德拉神庙环路附近”。匹配(格式)){
console.log(“无效字符”);
}否则{
console.log(“好”);

}
问题在于,您在其中有一个空间:

/[ \\|]/
实际上意思是“匹配任何一个空格,
\
\

如果您只想匹配管道,请使用以下方法:

const format=/[\\\124;]/;//这是用于匹配管道阵列的阵列
if(“拉格哈文德拉神庙环路附近”。匹配(格式)){
console.log(“无效字符”);
}否则{
console.log(“好”);

}
如果您以前没有使用过它,它非常适合解释正则表达式模式的语义。如果您以前没有使用过它,它非常适合解释正则表达式模式的语义。