Reputation: 9410
<font color="green">+4,13</font>%
I know that I shouldn't use regular expressions for that, but that's a single case where my only html is that so...
how can I get "4,13" from the string above?
EDIT
Context:
I am sorting a table via jQuery TableSorter. A column contains that html-formatted data and I can't change it. The custom parser I'm writing has a format function, which I currently use for managing currency, percentages and so on...
Now, I want to check, with a regex, if the string that comes to me is a string.
format: function(s) {
console.log(s);
var stripped = s.replace("<font>","")
.replace("</font>", "");
return jQuery.tablesorter.formatFloat(stripped);
}
Upvotes: 1
Views: 2084
Reputation: 93046
Edit
If you just want to match numbers
[+-]?\d+(?:[,.]\d+)?
See it here on Regexr
Matches for an optional + or - then at least one digit, then an optional fraction with a . or a , as fraction delimiter.
/Edit
Try something like this
<font color="green">([^<]+)
You will then find the value "+4,13" in the capturing group 1.
See it here on Regexr
If you want to exclude the +
than add it (maybe optional) before the capturing group
<font color="green">\+?([^<]+)
Upvotes: 0
Reputation: 24236
This should work for your specific example -
var tagtext = '<font color="green">+0.00</font>%';
var keepplusorminus = false;
var myregexp = keepplusorminus ? /[-\+,\.0-9]+/ : /[,\.0-9]+/;
var match = myregexp.exec(tagtext);
if (match != null) {
result = match[0];
} else {
result = "";
}
alert(result);
Working demo - http://jsfiddle.net/ipr101/LHBp7/
Upvotes: 1