jQuery remove special characters from string and more

JavascriptJqueryRegexReplace

Javascript Problem Overview


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';

Javascript Solutions


Solution 1 - Javascript

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: https://stackoverflow.com/questions/388996/regex-for-javascript-to-allow-only-alphanumeric

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

Solution 2 - Javascript

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

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

Solution 3 - Javascript

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

Solution 4 - Javascript

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

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

Demo

Solution 5 - Javascript

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, ""));

Solution 6 - Javascript

Since I can't comment on Jasper's answer, I'd like to point out a small bug in his solution:

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

The problem is that first code removes all the hyphens and then tries to replace them :) You should reverse the replace calls and also add hyphen to second replace regex. Like this:

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

Solution 7 - Javascript

Remove/Replace all special chars in Jquery :

If

str = My name is "Ghanshyam" and from "java" background

and want to remove all special chars (") then use this

str=str.replace(/"/g,' ')

result: > My name is Ghanshyam and from java background

Where g means Global

Solution 8 - Javascript

var str = "I'm a very^ we!rd* Str!ng.";
$('body').html(str.replace(/[^a-z0-9\s]/gi, " ").replace(/^\s+|\s+$|\s+(?=\s)/g, "").replace(/[_\s]/g, "-").toLowerCase());

First regex remove special characters with spaces than remove extra spaces from string and the last regex replace space with "-"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRoelView Question on Stackoverflow
Solution 1 - JavascriptJasperView Answer on Stackoverflow
Solution 2 - JavascriptIlia GView Answer on Stackoverflow
Solution 3 - JavascriptGrace ShaoView Answer on Stackoverflow
Solution 4 - JavascriptPawan Singh BishtView Answer on Stackoverflow
Solution 5 - JavascriptAmit SharmaView Answer on Stackoverflow
Solution 6 - JavascriptMarko SulamägiView Answer on Stackoverflow
Solution 7 - Javascriptghanshyam singhView Answer on Stackoverflow
Solution 8 - JavascriptUsman MalikView Answer on Stackoverflow