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
\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://bobbyhadz.com/blog/javascript-remove-non-alphanumeric-characters-from-string
©xgqfrms 2012-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/15987587.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需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