KevinT.
KevinT.

Reputation: 372

Replacing different characters with one

I have this code: .replace(/\ /g, "-"); that replaces every space with a dash. Now, how do I filter out non-letter characters to be replaced with a dash. I have tried .replace(/\ 12345/g, "-"); (a space and numbers 1-5) but it doesn't work for me.

Upvotes: 0

Views: 50

Answers (2)

David Xu
David Xu

Reputation: 5607

Try this:

.replace(/[ A-Za-z]/g, "-");

will replace A-Z, a-z and spaces.

Upvotes: 2

Mark Zhou
Mark Zhou

Reputation: 13

Try this

str.replace(/[^a-zA-Z]+/g, '-');

Upvotes: 0

Related Questions