JavaScript/REGEX: Inside a double set of parenthesis

Using JavaScript and Regular Expressions, how can I get the content inside a double set of parenthesis?

getInside2p('http://example.com/dir/(S(u53ouszaxo3dis55xhfm0l45))/page.aspx?xyz'); // should return 'u53ouszaxo3dis55xhfm0l45'
getInside2p('xyz((hello))123'); // should return 'hello'
getInside2p('Hello World'); // should return ''

Open in new window

LVL 10
skijAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

Pls try

\([^\)]*\((.*)\)[^\(]*\)

Open in new window

Regards
skijAuthor Commented:
Thanks, the REGEX is good, but I get an error if there is no match.

For example,
getInside2p('Hello World'); // should return ''

but that returns an error.

alert(getInside2p('http://example.com/dir/(S(u53ouszaxo3dis55xhfm0l45))/page.aspx?xyz'));
alert(getInside2p('xyz((hello))123'));
alert(getInside2p('hello'));

function getInside2p(str) {
 return(str.match(/\([^\)]*\((.*)\)[^\(]*\)/))[1];
}

Open in new window

Kim WalkerWeb Programmer/TechnicianCommented:
The [1] at the end of line 6 is likely causing the error. It is attempting to select the first value of an array that might be generated by the match statement. When there is no match, the match method will return a null value which is not an array, so the [1] designation throws an error. However, this match statement will never return an array, so the [1] designation is unnecessary. In order for the match statement to return an array, the global flag would have to be included in the expression.
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

skijAuthor Commented:
I realize that the problem is cause because of the [1] at the end, however if I don't put that at the end I don't get the desire results.   Please test this, thanks.
alert(getInside2p('http://example.com/dir/(S(u53ouszaxo3dis55xhfm0l45))/page.aspx?xyz'));
alert(getInside2p('xyz((hello))123'));
alert(getInside2p('hello'));

function getInside2p(str) {
 return(str.match(/\([^\)]*\((.*)\)[^\(]*\)/))[1];
}

Open in new window

Kim WalkerWeb Programmer/TechnicianCommented:
What are the desired results for your sample code? Are you wanting to limit the match to what is between the inside quotes only, or what is between the double or nested quotes?
skijAuthor Commented:
Here are the desired results:

getInside2p('/dir/(S(u53ouszaxo3dis55xhfm0l45))/page.aspx?xyz'); // should return 'u53ouszaxo3dis55xhfm0l45'
getInside2p('xyz((hello))123'); // should return 'hello'
getInside2p('Hello World'); // should return an empty result

I want to limit the match to only what is inside two layers of parenthesis and if there is no match the the result should be empty.
Kim WalkerWeb Programmer/TechnicianCommented:
There should be a simpler way to do this, but I just can't come up with one. Try this:
alert(getInside2p('http://example.com/dir/(S(u53ouszaxo3dis55xhfm0l45))/page.aspx?xyz'));
alert(getInside2p('xyz((hello))123'));
alert(getInside2p('hello'));

function getInside2p(str) {
	var outer,
		inner,
		trimmed = '',
		outerRegex = /\([^(]*\(([^()]*)+\)[^\)]*\)/,
		innerRegex = /\([^()]*\)/;
	if (outerRegex.test(str) ) {
		outer = str.match(outerRegex)[0];
		if (innerRegex.test(outer) ) {
			inner = outer.match(innerRegex)[0];
			trimmed = inner.substr(1,inner.length-2);
		}
	}
	return trimmed;
}

Open in new window

You're right, it returns an array when there is a match even without the global flag. But the first value in the array is at zero, not one. My mistake there as well. So it becomes necessary to test for a null value when there is no match or to simply test whether there is a match.

I've also been unable to come up with a single regular expression that would select only the characters inside the parentheses. So I ended up with three stages. The first stage produces the outer parentheses and all the characters between them. The second stage produces the inner parentheses and all the characters between them from the first stage. Finally, the first and last characters are trimmed using the substring method.

Hopefully this will produce the desired results.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.