[英]Javascript split string by multiple delimiters for logical expression
I want to split string by logical operators || 我想通过逻辑运算符来分割字符串|| and &&.
和&&。 For example true && false ||
例如,真&&假|| true = true,false,true in array... I tried with creating a temporary string where I replaced all ||s with &&s and then split by &&, but it doesnt suit my needs, so I am looking for better solution, maybe with regular expression or something.
true = true,false,true in array ...我尝试创建一个临时字符串,在其中我将所有||替换为&& s,然后由&&分割,但这不符合我的需求,因此我正在寻找更好的解决方案,也许带有正则表达式之类的东西 Also, character class in regex doesnt work, because it matches just single character, so if I use [&&||] it doesnt work.
另外,正则表达式中的字符类不起作用,因为它仅匹配单个字符,因此如果我使用[&& ||]则不起作用。
You mean this? 你是这个意思?
> "true && false || true".split(/\&\&|\|\|/)
[ 'true ', ' false ', ' true' ]
If you don't want the spaces, then try 如果您不想要空格,请尝试
> "true && false || true".split(/ \&\& | \|\| /)
[ 'true', 'false', 'true' ]
用这个:
thesplits = yourString.split(/&&|\|\|/);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.