最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Removing punctuation from strings? - Stack Overflow

programmeradmin1浏览0评论

I'm working on a palindrome function and have e 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!

I'm working on a palindrome function and have e 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!

Share Improve this question asked Aug 3, 2020 at 10:06 James RossJames Ross 771 silver badge7 bronze badges 4
  • Can you give some sample data and explain why the replacement is not happening as expected? – Tim Biegeleisen Commented Aug 3, 2020 at 10:09
  • 1 In your case, it would be far easier to write a pattern that includes what you do want rather than what you don't. If you want to allow only numbers and letters you can use str.replace(/\W/g, '') – Mitya Commented Aug 3, 2020 at 10:10
  • 1 Nothing changes in your result because spaceRE is here to remove all spaces or white space, and you have none in you input. Also wondering why you didn't simply use [^a-zA-Z0-9] instead of both regexes.. – Kaddath Commented Aug 3, 2020 at 10:10
  • If one of you would mind answering so I can mark solved. I've only started learning about regexes tonight so I'm not familiar with much. – James Ross Commented Aug 3, 2020 at 10:15
Add a ment  | 

2 Answers 2

Reset to default 5

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

Turns out var spaceRE = /\s+/g; removes all whitespaces from strings, while punctuation removes punctuation. Replacing both simultaneously with empty strings delivers a string with no punctuation or whitespaces and saves it to testStr

发布评论

评论列表(0)

  1. 暂无评论