33

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.

3
  • Use an escape character \- to specify the - inside the square brackets. This will make sure the parser interprets the - as a literal character and not as a special character indicating a range.
    – Hans Then
    Dec 5, 2012 at 15:22
  • What does jQuery have to do with this? Regex and string replacement are both parts of the language, jQuery doesn't get involved. Removing that tag; if I'm mistaken, feel free to rollback.
    – ssube
    Dec 5, 2012 at 15:36
  • Shouldn't this be tagged javascript, or at least mention javascript in the question somewhere? I had to look in the comments to find out it had previously been tagged jQuery to be sure it wasn't about some language with very similar syntax.
    – Ben
    May 3, 2018 at 8:28

5 Answers 5

73

This should work :

str = str.replace(/[^a-z0-9-]/g, '');

Everything between the indicates what your are looking for

  1. / is here to delimit your pattern so you have one to start and one to end
  2. [] indicates the pattern your are looking for on one specific character
  3. ^ indicates that you want every character NOT corresponding to what follows
  4. a-z matches any character between 'a' and 'z' included
  5. 0-9 matches any digit between '0' and '9' included (meaning any digit)
  6. - the '-' character
  7. g at 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 string

Then 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".

9

Just change + to -:

str = str.replace(/[^a-z0-9-]/g, "");

You can read it as:

  1. [^ ]: match NOT from the set
  2. [^a-z0-9-]: match if not a-z, 0-9 or -
  3. / /g: do global match

More information:

0
1

Your character class (the part in the square brackets) is saying that you want to match anything except 0-9 and a-z and +. You aren't explicit about how many a-z or 0-9 you want to match, but I assume the + means you want to replace strings of at least one alphanumeric character. It should read instead:

str = str.replace(/[^-a-z0-9]+/g, "");

Also, if you need to match upper-case letters along with lower case, you should use:

str = str.replace(/[^-a-zA-Z0-9]+/g, "");
0
str = str.replace(/\W/g, "");

This will be a shorter form

0

We can use /[a-zA-Z]/g to select small letter and caps letter sting in the word or sentence and replace.

var str = 'MM-DD-yyyy'
var modifiedStr = str.replace(/[a-zA-Z]/g, '_')

console.log(modifiedStr)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.