我在一堆变化很大的字符串上使用.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*\(([^()]+)
( # 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/