Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get text between brackets [duplicate]

I have this string : Charles de Gaulle, (Paris) [CDG]

I would like in JavaScript/jQuery get just Paris. The initial string can have variable length.

I have tried this:

var tab = "Charles de Gaulle, (Paris) [CDG]";
var tab2 = tab.split(','); 
var tab3 = tab2.split('[') 
like image 489
Mamadou Avatar asked Aug 22 '13 09:08

Mamadou


2 Answers

Try

var myString= "Charles de Gaulle, (Paris) [CDG]";
var result = myString.match(/\((.*)\)/);
alert(result[1]);  

DEMO

like image 59
Suresh Atta Avatar answered Nov 15 '22 07:11

Suresh Atta


You can use .slice(begin,end) instead of .substring().

For example click on the link to view your result: http://jsfiddle.net/qhZq5/

like image 36
Prats Avatar answered Nov 15 '22 05:11

Prats