Ali Demirci
Ali Demirci

Reputation: 5442

Unable to replace with javascript regex

I've notifications system on my app and document title is updating with addition of total number of notifications just like facebook and twitter. I want it to remove number with parantheses just after user's click to a button.

document.title = dc.replace(/\(([^)][\d]+)\)/, "");

this is what I use but it's not working. i'm not that good with regex. All I want to do is replace (10) My Website's Homepage with My Website's Homepageon click.

thanks.

Upvotes: 0

Views: 72

Answers (2)

user1726343
user1726343

Reputation:

I'm guessing you meant for the [^)] to be a lookahead instead of an actual character, and that the + quantifier is supposed to go outside the parentheses that groups this lookahead to the digit. That is, you're trying to say:

* Find an opening bracket
* Followed by one or more digits that aren't closing brackets
* Followed by a closing bracket

But notice how the second sentence, although valid, is redundant? A digit is never a closing bracket. All you need is therefore:

/\(\d+\)/

Upvotes: 2

Amit Joki
Amit Joki

Reputation: 59292

This should work.

document.title=dc.replace(/\(\d+\)\s/,"");

Demo:http://jsfiddle.net/x6BKD/

Upvotes: 2

Related Questions