JavaScript/REGEX: Inside a double set of parenthesis

skij
skij used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
Hi,

Pls try

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

Open in new window

Regards

Author

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/Technician

Commented:
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.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

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/Technician

Commented:
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?

Author

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.
Web Programmer/Technician
Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial