Search code examples
javascriptregexescript

remove special characters from text


I am trying to remove special characters from a given text.

I've tried to replace it with:

var stringToReplace = 'ab風cd  👰� abc 123 € AAA';
var newText = stringToReplace.replace(/[^\d.-]/g, '');

but it's not working. I want that the newText will be:

newText = 'ab cd      abc 123   AAA'

I've tried some regex but none of them seem to work. the real problem is with '風' (those kind of characters). any suggestion? Thanks!


Solution

  • Just use.

    stringToReplace.replace(/\W/g, ' ');
    

    Output

    "ab cd abc 123 AAA"