I am trying to do a basic string replace using a regex expression, but the answers I have found do not seem to help – they are directly answering each persons unique requirement with little or no explanation.
I am using str = str.replace(/[^a-z0-9+]/g, ''); at the moment. But what I would like to do is allow all alphanumeric characters (a-z and 0-9) and also the ‘-‘ character.
Could you please answer this and explain how you concatenate expressions.
This should work :
Everything between the indicates what your are looking for
/is here to delimit your pattern so you have one to start and one to end[]indicates the pattern your are looking for on one specific character^indicates that you want every character NOT corresponding to what followsa-zmatches any character between ‘a’ and ‘z’ included0-9matches any digit between ‘0’ and ‘9’ included (meaning any digit)-the ‘-‘ charactergat the end is a special parameter saying that you do not want you regex to stop on the first character matching your pattern but to continue on the whole stringThen your expression is delimited by
/before and after.So here you say “every character not being a letter, a digit or a ‘-‘ will be removed from the string”.