0

I have a string that can look like either of these Adjuster, Carrier 3 (Carrier 3) or Adjuster, Carrier 3 (Carrier 3 (Test))

I want to capture the contents within the first set of parentheses. My original regex pattern was \((.+?)\) (non-greedy), so I can capture the text as group #1.

var selectedOwnerText = /* GET THE TEXT FROM NODE/FIELD/ETC. */, 
    carrierName = '',
    rePattern = /\((.+?)\)/;

    if (selectedOwnerText != '') {
       carrierName = selectedOwnerText.match(rePattern);
       if (carrierName != null) {
          carrierName = carrierName[1];
       }
    }
 // Rest of code...

This works in the first text case, but in the second text case it grabs the outer parentheses e.g. (Carrier 3 (Test).

Is there a regex pattern that can capture the text inside the outer parentheses, which may include parentheses as well? I want either Carrier 3 or Carrier 3 (Test) extracted from the above.

EDIT: I have just been told that this data field is free-form text, so anything could appear inside the outer parentheses. So, I would need to capture everything inside the outer parentheses.

EDIT 2: I gave one user the correct answer, because it answered the original question (assuming only one set of inner parentheses). Now that I know the text could be anything, a Javascript regex pattern is impossible, and I abandoned the regex approach. I dived into the server-side code and surfaced a JSON literal of the data I needed, so as the page/Javascript is being created, I can use the data structure to get the content I need without worrying about what the string actually looks like. Thanks to all who tried to help!

8
  • Is the nesting depth limited? Commented Sep 18, 2013 at 14:52
  • How many level of nesting? Limited levels can be parsed, but arbitrary levels is not possible with JS regex. Commented Sep 18, 2013 at 14:52
  • stackoverflow.com/questions/15910158/… The question is in Groovy (Java), but it also applies for JS. Commented Sep 18, 2013 at 14:53
  • Are all your strings in the format word, word (anything inside) and are you testing each of the strings separately? Commented Sep 18, 2013 at 14:54
  • 1
    Have you tried this rePattern = /((.+))/; ? Commented Sep 18, 2013 at 14:59

1 Answer 1

1

You can just use the greedy version if and only if your string has the format word, word (anything inside) and not word, word (anything inside) word (more parenthesized stuff):

/\((.+)\)/;

You'll get everything within the first opening parenthesis and the last closing parenthesis.

regex101 demo

Sign up to request clarification or add additional context in comments.

7 Comments

More generally, this solution assumes a single pair of outer parentheses.
I was told that "word, word (anything inside) word (more parenthesized stuff)" could happen since it is a free-form text field, although that is an edge case. I may have to find another way to get at that specific piece of data than to rely on text in the DOM node and assume only one pair of outer parentheses.
Oh, well, if people are even allowed to put unbalanced parens, then that's even worse! With missing brackets and multiple outer parens... it'd be a mess I don't think even a parser could handle.
Jerry, I'm giving you the correct answer, because it solves my original problem. Because I just found out that the data I'm dealing with is free-form, I abandoned the regex approach. I dived into the server-side code and surfaced a JSON literal of the data I needed, so as the page/Javascript is being created, I can use the data structure to get the content I need without worrying about what the string actually looks like.
@Stephen Thank you. I hope things turn out well for you. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.