简体   繁体   English

Javascript通过多个定界符将字符串拆分为逻辑表达式

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

相关问题 用多个定界符分割字符串,并忽略引号中的定界符javascript - Split a string by multiple delimiters and ignore delimiters in quotes javascript JavaScript字符串使用多个定界符进行拆分,同时保留定界符 - JavaScript String split with multiple delimiters while keeping the delimiters 通过 Javascript 中的成对分隔符拆分字符串,多次出现并排除 - Split string by paired delimiters in Javascript, on multiple occurrences, and with exclusions 在 JavaScript 中组合多个分隔符(拆分) - Combining multiple delimiters (split) in JavaScript 根据多个分隔符 [/, #, @, ''] 拆分字符串 - Split a string based on multiple delimiters [/, #, @, ''] 根据多个分隔符拆分字符串 - Split a string based on multiple delimiters 用javascript中的多个定界符分割方程式字符串,并保留定界符,然后将字符串放回一起 - split equation string by multiple delimiters in javascript and keep delimiters then put string back together 通过多个分隔符拆分字符串并保留一些分隔符而丢弃其他分隔符 - Split string by multiple delimiters and keeping some delimiters while discarding others 根据开始和结束分隔符在javascript中拆分字符串 - split a string in javascript based on start and end delimiters javascript:将字符串拆分为具有不同定界符的数组 - javascript: split a string into an array with different delimiters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM