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!
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!
Share Improve this question edited Jun 29, 2011 at 14:44 trashgod 206k33 gold badges261 silver badges1.1k bronze badges asked Jun 29, 2011 at 9:56 netcasenetcase 1,3593 gold badges13 silver badges15 bronze badges 1- You might want to use indexOf() and substring() like this: var test = push($(this).text(); test = test.substring(test.indexOf('('),test.length-1); should work if you always have that named pattern. – Andresch Serj Commented Jun 29, 2011 at 10:02
3 Answers
Reset to default 13If 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"
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 )'
Look here: Using jQuery to find a substring
But substring(), indexof() might be easier than regex.
http://www.w3schools.com/jsref/jsref_indexof.asp