Following this question How can I strip all punctuation from a string in JavaScript using regex? I'm trying to remove all punctuation from a string besides the '
character to avoid messing with words such as:
So far I have tried the following:
return word.replace(/[^\w\s]|_\'/g, "")
.replace(/\s+/g, " ");;
But it is still removing the '
character. Any idea how I can achieve such a result?
将转义的撇号移动到排除的字符类: /[^\\w\\s\\']|_/g
您可以使用此正则表达式
console.log("I'm strong !".replace(/[.,\\/#!$%\\^&\\*;:{}=\\-_`~()]/g,""))
Add the apostrophe to your regex like this, in the exclusion block:
const str = "This., -/is #! a ;: {} = - string$%^&* which `~)() doesn't, have any)( punctuation!"; const noPunctation = str.replace(/[^\\w\\s']|_/g, '').replace(/\\s+/g, ' '); console.log(noPunctation);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.