Crezzer7 Crezzer7 - 1 month ago 7
Javascript Question

REGEX - after bracket get data until end bracket

I have a string like the following:

SOME TEXT (BI1) SOME MORE TEXT (BI17) SOME FINAL TEXT (BI1234)


Question

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:
/\(([^)]+)\)/g
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:
http://www.regexr.com

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

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).

Comments