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!开发者_Go百科
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"
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
精彩评论