xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js regexp remove all non-alphanumeric characters All In One

js regexp remove all non-alphanumeric characters All In One

alphanumeric characters / word character

const str = '123abc x . / m';
// 正向处理 \W
const pureStr = str.replace(/[\W_]+/g, '');
// '123abcxm'
const str = '123abc x . / m';
// 反向处理 ^
const pureStr = str.replace(/[^0-9A-Za-z]/g, ``).toLowerCase();
// '123abcxm'

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Character_Classes#types

\W

Matches any character that is not a word character from the basic Latin alphabet.
Equivalent to [^A-Za-z0-9_].

For example, /\W/ or /[^A-Za-z0-9_]/ matches "%" in "50%" and "É" in "Émanuel".

匹配任何不是来自基本拉丁字母的单词字符的字符。
等价于 [^A-Za-z0-9_]

demo

const p = `
	The quick brown fox jumps over the lazy dog. 
    If the dog reacted, was it really lazy?
`;

console.log(p.replaceAll('dog', '👻'));


// global flag required when calling replaceAll with regex
const regex = /Dog/ig;
console.log(p.replaceAll(regex, '💩'));

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll

refs

https://stackoverflow.com/questions/20864893/replace-all-non-alphanumeric-characters-new-lines-and-multiple-white-space-wit

https://bobbyhadz.com/blog/javascript-remove-non-alphanumeric-characters-from-string



©xgqfrms 2012-2025

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(36)  评论(2编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
历史上的今天:
2021-03-09 Object Deep Copy All In One
2021-03-09 element-ui 的 table 数据 id 重复,如何渲染重复的 id 行 和 单选 radio checkbox 解决方案
2021-03-09 js array remove item All In One
2021-03-09 广告系统-开发文档 All In One
2020-03-09 HTML5 export excel All In One
2020-03-09 js destructuring assignment bug
2019-03-09 DevOps
点击右上角即可分享
微信分享提示