Hi everyone, I’m trying to do something similar to Glitch’s project naming, where all special characters are replaced with dashes. I’m attempting to use regExp, but for some reason it seems to ignore the last character. What I mean by this is if I type in Hello!
it will stay the same. If I then type in an extra letter to make Hello!x
it will become Hello
. Doing this without regExp, e.g.
string.replace(" ", "-")
works fine.
Here’s the code I’m using:
string = string.replace(/[^qwertyuioplkjhgfdsazxcvbnm-][^0-9]/ig, "-");
Obviously string
is defined.
Any help would be greatly appreciated!