Regex - Find All Occurrences Within a String

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

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

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

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

3

2 Answers

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' ]
1
  \[(.*?)\]

This should be your fancy regex.

See demo.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest