javascript regex - 查找字符串中所有出现的地方

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25546185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:39:10  来源:igfitidea点击:

regex - find all occurrences within a string

javascriptjqueryregex

提问by Rich

I have a string: "[2-0]>5&&[3-0]<21"

我有一个字符串:“[2-0]>5&&[3-0]<21”

I would like to pull out an array of: 2-0 and 3-0

我想拉出一个数组:2-0 和 3-0

The resulting array should look like this: ["2-0", "3-0"]

结果数组应如下所示: ["2-0", "3-0"]

Does anyone know some fancy regex that will do this, or perhaps another method?

有谁知道一些花哨的正则表达式可以做到这一点,或者可能是另一种方法?

采纳答案by Avinash Raj

You could try the below code which matches the strings present inside the []brackets,

您可以尝试使用以下代码匹配[]括号内的字符串,

> "[2-0]>5&&[3-0]<21".match(/[^\[\]]+(?=\])/g)
[ '2-0', '3-0' ]

回答by vks

  \[(.*?)\]

This should be your fancy regex.

这应该是你喜欢的正则表达式。

See demo.

见演示。

http://regex101.com/r/yZ7hR7/1

http://regex101.com/r/yZ7hR7/1