Brandon Korenek
Brandon Korenek

Reputation: 174

Regex, removing recurring characters but keeping at least one

I'm using this

html = html.replace(/([^0-9]).*?\1/ , "");

but it's not quite doing what I'm trying to do. I want to replace the doubles within a string, but still keep at least one of them. Can't seem to figure out how to go about this.

Upvotes: 0

Views: 127

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174706

THink you mean this,

str.replace(/(.)\1+/g, '$1')

or

str.replace(/([^0-9])\1+/g, '$1')

Upvotes: 3

Related Questions