Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - get values inside parentheses

I got a HTML Table with values inside each row.
The datas inside the consist the following format:

ABCD (1000.50)

$('.tdclass').each(function(index){
  var test = push($(this).text().match(?regex?));
});

Well, regex is definitely not one of my strength ;-)
What is the according regex to get the values inside the parentheses?

Any help would be appreciated!

like image 660
netcase Avatar asked Jun 29 '11 09:06

netcase


2 Answers

If the first part of a string is a fixed length, you can avoid complicated procedures entirely using slice():

var str = "ABCD (1000.50)";

alert(str.slice(6, -1));
//-> "1000.50"

Otherwise, you can use indexOf() and, if you need it, lastIndexOf() to get the value:

var str = "ABCDEFGHIJKLMNO (1000.50)",
    pos = str.indexOf("(") + 1;

alert(str.slice(pos, -1));
//-> "1000.50"

alert(str.slice(pos, str.lastIndexOf(")");
//-> "1000.50"
like image 111
Andy E Avatar answered Oct 19 '22 06:10

Andy E


A minimal regex of \((.*)\) would do the trick, however this doesn't account for multiple brackets, and there's no need to include 4 characters prior to that. It literally says 'match (, followed by as many non-new-line characters as possible, followed by )'

like image 25
Simon Scarfe Avatar answered Oct 19 '22 06:10

Simon Scarfe