javascript - Removing punctuation from strings? - Stack Overflow

I'm working on a palindrome function and have e across a formula which removes punctuation from st

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745134390a4613129.html

相关推荐

  • javascript - Removing punctuation from strings? - Stack Overflow

    I'm working on a palindrome function and have e across a formula which removes punctuation from st

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信