Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing punctuation from strings?

I'm working on a palindrome function and have come across a formula which removes punctuation from strings.


var punctuation = /[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-.\/:;<=>?@\[\]^_`{|}~]/g;
var spaceRE = /\s+/g;
var str = "randomstringwith*&^%"
var testStr = str.replace(punctuation, '').replace(spaceRE, '')
document.write(testStr);

My question is that If I remove the .replace(spaceRE, '') nothing seems to change in the result. Is there something I'm missing or does this formula have excess code on it? also I'm slightly confused about the use of str.replace(punctuation,'');

punctuation represents any non letter/number characters and the '' replaces them with an empty string, correct? Thanks!

like image 865
James Ross Avatar asked Jun 18 '26 03:06

James Ross


1 Answers

In situations like yours you have to ask yourself which is easier:

  • Create a REGEXP that blocks certain characters
  • Create a REGEXP that allows certain characters

The choice you opt for should depend on which is less work and be more reliable.

Writing a pattern that blocks all symbols depends on you remembering every possible symbol - not just punctuation, but emoji patterns, mathematical symbols and so on.

If all you want is to allow numbers and letters only, you can do:

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

\W/ is an alias for "non-alphanumeric" characters. The only caveat here is alphanumeric includes underscores, so if you want to block those too:

str.replace(/\W|_/g, '');
like image 104
Mitya Avatar answered Jun 19 '26 17:06

Mitya