简体   繁体   English

正则表达式仅接受0,1,(,,, |,&&

[英]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.

相关问题 正则表达式只接受正数和小数 - Regular Expression to accept only positive numbers and decimals Javascript正则表达式只接受特定字符 - Javascript regular expression only accept specific characters 只接受字母和空格的正则表达式 - Regular expression that ONLY accept letters and white spaces 正则表达式只接受字符串第 1 位的字母 - Regular Expression to accept only alphabets in the 1st place of the string 如何使正则表达式只接受特殊公式? - How to make regular expression only accept special formula? JavaScript正则表达式-仅接受两个不带特殊字符的单词 - JavaScript Regular Expression - Accept only two words with no special characters 无效的正则表达式:“/^[+]?[0-9]{0,1}[-.]?\(?([0-9]{3})\)?[-.]?([0 -9]{3})[-. ]?([0-9]{4})$/gm" 在 javscript - Invalid regular expression: "/^[+]?[0-9]{0,1}[-. ]?\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/gm" in javscript 输入类型数字的正则表达式只需要 10 位数字吗? 只接受整数 - Regular Expression for input type number want 10 digit only? Only accept whole number 正则表达式仅接受正数和小数(仅以0.5 .0或整数接受) - Regular Expression to accept only positive number and decimals (only end with .5 .0 or integer accepted) Java脚本中的正则表达式仅接受0-9连字符和字母之间的数字,不带空格 - regular expression in java-script to accept only numbers from 0-9 hyphen and alphabet with no space
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM