[英]Regular Expression to accept only 0,1,(,),|,&
I am trying to create a regular expression that contains only below 6 characters in a string 我正在尝试创建一个仅在字符串中包含6个以下字符的正则表达式
0,1,(,),|,& 0,1,(,),|,&
I came up with this regular expression 我想出了这个正则表达式
^[0-1\\|()\\&]$ ^ [0-1 \\ |(()\\&] $
But it seems to fail even when valid string is given as input. 但是,即使给定有效字符串作为输入,它似乎也会失败。 As below 如下
const inputStr = "(0&(1&(1|0))";
const validateStr = new RegExp("^[0-1\|\(\)\&]$");
const isValid = validateStr.test(inputStr);
Above need to be returned true, but the output of isValid is false. 上面需要返回true,但是isValid的输出为false。 Please let me know where I am doing wrong. 请让我知道我在哪里做错了。
You need to add a plus after your character set to search the whole string! 您需要在字符集之后添加一个加号来搜索整个字符串!
const inputStr = "(0&(1&(1|0))"; const validateStr = new RegExp(/^[0-1()|&]+$/); const isValid = validateStr.test(inputStr); console.log(isValid);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.