2

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.

1

2 Answers 2

5

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/

Sign up to request clarification or add additional context in comments.

Comments

-2

use this one:

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

1 Comment

You're also missing the capturing group for the number which is what the OP is actually after

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.