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.

有帮助吗?

解决方案

Try this:

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

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

其他提示

Try this

str.replace(/[^a-zA-Z]+/g, '-');
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top