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!
- 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
2 Answers
Reset to default 5In 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
评论列表(0条)