netcase
netcase

Reputation: 1359

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!

Upvotes: 9

Views: 11254

Answers (3)

Simon Scarfe
Simon Scarfe

Reputation: 9618

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 )'

Upvotes: 1

Andy E
Andy E

Reputation: 344517

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"

Upvotes: 13

Tim
Tim

Reputation: 5421

Look here: Using jQuery to find a substring

But substring(), indexof() might be easier than regex.

http://www.w3schools.com/jsref/jsref_indexof.asp

Upvotes: 0

Related Questions