Regexp For Numbers In The Thousands
Solution 1:
I have never seen this way of using regular expressions. But this should do it:
/Posts: (\d+(?:,\d+)?)/
This wouldn't match 1,000,000
though, if you want to cover all cases, change ?
to *
:
/Posts: (\d+(?:,\d+)*)/
Solution 2:
More grist for the mill... if you want to test for numbers with thousands seperators excatly, you can do something like:
var matches = td[i].innerHTML.match(/\d{1,3}(,\d{3})*/);
postCount = matches? matches[0] : 0;
It may be better to use textContent/innerText than innerHTML since you might want to remove markup. A simple function can be:
functiongetText(el) {
if (typeof el.textContent == 'string') {
return el.textContent;
} elseif (typeof el.innerText == 'string') {
return el.innerText;
}
}
Solution 3:
This should do the trick:
Posts: (\d*,?\d+)
Edit: If you want to match 1,000,000 (like Felix Kling demonstrates), you need a repeating capturing group for the upper part:
Posts: ((?:\d+,)*\d+)
Solution 4:
If you want to capture the commas, just change your pattern to:
Posts: (\d+(,\d{3})*)
If you don't want the commas, you'll need an extra step. Someone help me out with the JS syntax, but I suppose it might be:
postCount = RegExp.$1.replace(/,/g, '')
Solution 5:
If they are using a comma number format such as 1,000,000,000 then the following regex should work nicely:
$posts = "Posts: 10,000,000";
$pat = "/Posts: (\d+(?:,\d{3})*)/";
preg_match($pat, $posts, $matches);
$post_count = $matches[1];
Post a Comment for "Regexp For Numbers In The Thousands"