我试图在JavaScript中使用Regex,它匹配一个字符串并返回3个组: operand1、operator和operand2。例如,剖析指数子句:
const EXPONENT_REGEX = /(\S+)\s*([\^])\s*(\S+)/
const inpStr = '5 ^ 2 + 10 * 2'
inpStr.match(EXPONENT_REGEX)
// Returns: ['5 ^ 2', '5', '^', '2']
// i.e. [Matched Clause, Operand1, Operator, Operand2] 但是,在尝试类似的方法来处理括号子句时,以及可能的嵌套括号--我一直无法找到正确的转义方括号和括号的排列方式,以便分组以返回这3个值。考虑:
const PARENTHESES_REGEX = /\([^\(\)]*\)/
const inpStr = '5 ^ 2 + 10 * (10 - (4 + (2 + 1))'
inpStr.match(PARENTHESES_REGEX)目前,这并不返回任何组,而是匹配“(2+1)”。我该如何构造它来返回第一个示例中相同的3个组呢?在这里看起来应该是:
'2 + 1','2','+','1‘
发布于 2022-02-19 22:52:51
使用
const PARENTHESES_REGEX = /\((-?\d*\.?\d+)\s*([-+\/*])\s*(\d*\.?\d+)\)/g
const inpStr = '5 ^ 2 + 10 * (10 - (4 + (2 + 1))'
console.log([...inpStr.matchAll(PARENTHESES_REGEX)])
解释
--------------------------------------------------------------------------------
\( '('
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
-? '-' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\d* digits (0-9) (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\.? '.' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
[-+\/*] any character of: '-', '+', '\/', '*'
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \3:
--------------------------------------------------------------------------------
\d* digits (0-9) (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\.? '.' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
) end of \3
--------------------------------------------------------------------------------
\) ')'结果:(4) ['(2 + 1)', '2', '+', '1', index: 24, input: '5 ^ 2 + 10 * (10 - (4 + (2 + 1))', groups: undefined]
https://stackoverflow.com/questions/71019488
复制相似问题