首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用regex和括号返回3个组:operand2 1/operator/operand2 2

使用regex和括号返回3个组:operand2 1/operator/operand2 2
EN

Stack Overflow用户
提问于 2022-02-07 13:36:32
回答 1查看 47关注 0票数 1

我试图在JavaScript中使用Regex,它匹配一个字符串并返回3个组: operand1、operator和operand2。例如,剖析指数子句:

代码语言:javascript
复制
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个值。考虑:

代码语言:javascript
复制
const PARENTHESES_REGEX = /\([^\(\)]*\)/
const inpStr = '5 ^ 2 + 10 * (10 - (4 + (2 + 1))'
inpStr.match(PARENTHESES_REGEX)

目前,这并不返回任何组,而是匹配“(2+1)”。我该如何构造它来返回第一个示例中相同的3个组呢?在这里看起来应该是:

'2 + 1','2','+','1‘

EN

回答 1

Stack Overflow用户

发布于 2022-02-19 22:52:51

使用

代码语言:javascript
复制
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)])

解释

代码语言:javascript
复制
--------------------------------------------------------------------------------
  \(                       '('
--------------------------------------------------------------------------------
  (                        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]

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71019488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档