Reputation: 35
I need to search for the 1st occurence of a string in an html document that contains the following symbols: "Inbox (15)". I wrote the following expression which returns null, though:
var openTickets = document.body.innerHTML.match(/Inbox\s\((d+)\)/);
I only need the number (in the example above, it's 15) for further manipulations which is why I put it between capturing parentheses; I also escaped the "(" symbol (should have I?) as it's also present in the string.
Could anyone please help me get the regEx work? Thanks.
Upvotes: 2
Views: 110
Reputation: 164813
You're simply missing the escape sequence for numbers. Where you have d+
, you want \d+
.
Unless you need to support whitespace characters other than space (tab, newline, etc), I would just use
/Inbox \((\d+)\)/
I think this might perform a little better as you can eliminate the HTML tags and just parse the text
var rx = /Inbox \((\d+)\)/;
var str = document.body.textContent || document.body.innerText;
var num = rx.test(str) && rx.exec(str)[1];
Demo - http://jsfiddle.net/rMAHC/2/
Upvotes: 5
Reputation: 1451
use this one:
document.body.innerHTML.match(/Inbox\s+\([0-9]+\)/);
Upvotes: -2