MousseDeHumus
MousseDeHumus

Reputation: 35

Javascript match(), regex

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

Answers (2)

Phil
Phil

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+)\)/

Update

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

Gaurav Singla
Gaurav Singla

Reputation: 1451

use this one:

document.body.innerHTML.match(/Inbox\s+\([0-9]+\)/);

Upvotes: -2

Related Questions