jQuery remove special characters from string and more

169,953

Solution 1

replace(/[^a-z0-9\s]/gi, '') will filter the string down to just alphanumeric values and replace(/[_\s]/g, '-') will replace underscores and spaces with hyphens:

str.replace(/[^a-z0-9\s]/gi, '').replace(/[_\s]/g, '-')

Source for Regex: RegEx for Javascript to allow only alphanumeric

Here is a demo: http://jsfiddle.net/vNfrk/

Solution 2

Assuming by "special" you mean non-word characters, then that is pretty easy.

str = str.replace(/[_\W]+/g, "-")

Solution 3

str.toLowerCase().replace(/[\*\^\'\!]/g, '').split(' ').join('-')

Solution 4

Remove numbers, underscore, white-spaces and special characters from the string sentence.

str.replace(/[0-9`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi,'');

Demo

Solution 5

this will remove all the special character

 str.replace(/[_\W]+/g, "");

this is really helpful and solve my issue. Please run the below code and ensure it works

var str="hello world !#to&you%*()";
console.log(str.replace(/[_\W]+/g, ""));
Share:
169,953
Roel
Author by

Roel

Updated on July 08, 2022

Comments

  • Roel
    Roel almost 2 years

    I have a string like this:

    var str = "I'm a very^ we!rd* Str!ng.";
    

    What I would like to do is removing all special characters from the above string and replace spaces and in case they are being typed, underscores, with a - character.

    The above string would look like this after the "transformation":

    var str = 'im-a-very-werd-strng';
    
  • Roel
    Roel over 12 years
    I want to remove the non-word characters (or replace them with 'nothing'). I want to keep the numbers and normal letters and want to replace the spaces and underscores with a horizontal stripe.
  • Ari
    Ari about 10 years
    That's the most powerful answer. Simple yet powerful..!
  • Artjom B.
    Artjom B. over 9 years
    Comment from Daniel Przybylowski: It seems that the underscore is removed by the first regex. So the second one could be like: replace(/\s{1,}/g, '-') The reason for a quantifier is to replace one or more spaces with '-'. Why ? Imagine string like "something & something".
  • radu.luchian
    radu.luchian over 9 years
    Worth pointing out that unlike all the above answers this one actually deals with "foo & bar" like this "foo-bar" and not like this "foo--bar". Short, simple and does the job perfect!
  • Yass
    Yass almost 9 years
    Thanks a bunch for this!
  • Jasper
    Jasper over 8 years
  • Rossitten
    Rossitten about 8 years
    replace(/[_\s+]/g, '-') to replace multiple spaces with only one dash could come in handy
  • user3304007
    user3304007 over 6 years
    Will it work on word characters but non utf 8 ? Like ç ş ğ ü ö
  • heySushil
    heySushil over 4 years
    Thanks, @Jasper it's working perfectly. But if in string have more than one - (dash) then how to remove second dash.
  • Yunnosch
    Yunnosch about 3 years
    While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.