Remove all special characters except space from a string using JavaScript

Viewed 599775

I want to remove all special characters except space from a string using JavaScript.

For example, abc's test#s should output as abcs tests.

13 Answers

search all not (word characters || space):

str.replace(/[^\w ]/, '')

Try to use this one

var result= stringToReplace.replace(/[^\w\s]/g, '')

[^] is for negation, \w for [a-zA-Z0-9_] word characters and \s for space, /[]/g for global

const str = "abc's@thy#^g&test#s";
console.log(str.replace(/[^a-zA-Z ]/g, ""));

With regular expression

let string  = "!#This tool removes $special *characters* /other/ than! digits, characters and spaces!!!$";

var NewString= string.replace(/[^\w\s]/gi, '');
console.log(NewString); 

Result //This tool removes special characters other than digits characters and spaces

Live Example : https://helpseotools.com/text-tools/remove-special-characters

Try this:

const strippedString = htmlString.replace(/(<([^>]+)>)/gi, "");
console.log(strippedString);

const input = `#if_1 $(PR_CONTRACT_END_DATE) == '23-09-2019' # 
Test27919<alerts@imimobile.com> #elseif_1 $(PR_CONTRACT_START_DATE) ==  '20-09-2019' #
Sender539<rama.sns@gmail.com> #elseif_1 $(PR_ACCOUNT_ID) == '1234' #
AdestraSID<hello@imimobile.co> #else_1#Test27919<alerts@imimobile.com>#endif_1#`;
const replaceString = input.split('$(').join('->').split(')').join('<-');


console.log(replaceString.match(/(?<=->).*?(?=<-)/g));

Related