首页 > 解决方案 > Trying to come up with Regex Expression

问题描述

I am trying to parse this string: *(!((stunned)||dead))

Currently have

// ["*", "!", "stunned", "||dead"]
string.match(/[^()]+/g)

Trying to get ["*", "!", "stunned", "||", "dead"]

Ideally in a way that would also separate it regarless of placement "||dead" or "dead||"

while support ||, &&, ==

标签: javascriptregex

解决方案


Perhaps you want to either match word characters, or non-word characters which are not parentheses:

const str = '*(!((stunned)||dead))';
console.log(str.match(/\w+|[^()\w]+/g));


推荐阅读