javascript - 匹配正则表达式 : [everything before number], [数字]

标签 javascript jquery regex match

我在一堆变化很大的字符串上使用.match()。我想从字符串中获取括号内的最后一个数字,然后从那里获取之前的所有内容。例如:

1 serving  (57.0 g)
1 slice, small (2" x 2-1/2" x 1-3/4")  (32.0 g)

成为数组:

['1 serving', '57.0 g']
['1 slice, small (2" x 2-1/2" x 1-3/4")', '32.0 g']

各种字符串的列表:http://regex101.com/r/jJ5sF3/1

我一直在努力编写一个正则表达式来捕获这一点。

最佳答案

使用此模式

(.*\S)\s*\(([^()]+)  

Demo

(               # Capturing Group (1)
  .             # Any character except line break
  *             # (zero or more)(greedy)
  \S            # <not a whitespace character>
)               # End of Capturing Group (1)
\s              # <whitespace character>
*               # (zero or more)(greedy)
\(              # "("
(               # Capturing Group (2)
  [^()]         # Character not in [^()]
  +             # (one or more)(greedy)
)               # End of Capturing Group (2)

关于javascript - 匹配正则表达式 : [everything before number], [数字],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27117762/

相关文章:

Python Regex 匹配 YAML Front Matter

Javascript 正则表达式 - exec 无限循环

javascript - 使用 Materialise 和 Meteor 时应该在哪里初始化 select?

javascript - jQuery.data 不再适用于窗口?

javascript - 如何仅在 HTML 表单验证成功时执行一组 JavaScript 代码

regex - 使用RegEx获取输入页面的错误处理

javascript - 选择 n :th child from the bottom with jQuery?

javascript - 一个 JSON 对象分成几个集合对象

javascript - 如何使用Theseus 调试 angularjs 应用程序?

javascript - Express:将 req.body 对象中的数组分配给各个变量