Regular expression - exec

Hello hello!

the regular expression below retrieves the number 4, but how do I change it to find the value in the second bracket?

Please help me - thanks!

var a = 'sitepoint[4][8]';
var b = /\[(\w+)\]/.exec(a)[1];  
alert(b);

Hi RedBishop,

You could do this:

var b = /\[(\w+)\]$/.exec(a)[1]; 

This might or might not be a good solution depending on what you are trying to do.

RegExp.exec() only finds the first occurrence.

1 Like

@James_Hibbard,

Hey Pullo, thanks a lot, that works well.

@Dormilich,

Ok, thanks.

Hope you all have a good weekend. :smile: See you.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.