Crezzer7 Crezzer7 - 2 years ago 83
Javascript Question

REGEX - after bracket get data until end bracket

I have a string like the following:



I am trying to make a regex to get just the information between the curly brackets, for example the end string would look like:

BI1 BI17 BI1234

I have found this example on stackoverflow which will get the first value BI1, but will ignore the rest after.
Get text between two rounded brackets

this is the REGEX I created from the above link:
but it includes the brackets, I want to remove these.

enter image description here

I am using this website to attempt to solve this query which has a testing window to see if the regex entered works:

Additional Information

  • there can be any amount of numbers also, which is why I have given 3 different examples.

  • this is a continous string, not on seperate lines

thanks for any help on this matter.

Answer Source

While this isn't possible using just regexes, you can do it with string#split and the following regex:


Yielding code that looks a bit like this:

function getBracketed(str) {
    return str.split(/\).*?\(|^.*?\(|\).*?$/).filter(Boolean);

(You need to filter out the empty strings that'll appear at the beginning and end if you do it this way - hence the extra operation).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download