只允许使用字母数字字符的 JavaScript 正则表达式
本文介绍如何从字符串中删除所有非字母数字字符。你可以使用 RegEx
表达式匹配除 JavaScript 中的数字或字母之外的所有字符。
JavaScript 的正则表达式只允许字母数字字符
你将使用给定的正则表达式来验证用户输入以仅允许字母数字字符。字母数字字符是所有字母和数字,即字母 A-Z、a-z 和数字 0-9。
RegEx
的语法如下所示。
Regex: ^[a - zA - Z0 - 9] + $
有了我们可以使用的字母数字 RegEx
,使用 RegEx
只允许字符串中的字母数字字符的解决方案变得非常简单。
你可以设置一个启用一系列字符的字符类,并添加一个重复字符类一次或多次的量词以及将匹配项绑定到字符串开头和结尾的锚点。
要从字符串中删除非字母数字字符,你将首先调用 replace()
方法并传递一个匹配所有非字母数字字符的正则表达式 (RegEx)
作为第一个参数,并将一个空字符串作为第二个参数.
str.replace()
方法将返回一个替换所有字符的新字符串。
可以通过以下方式完成。
// a string
const str = '#BLOCTAKSolutions123?%';
// regex expression is used to match all non-alphanumeric characters in a string
const regex = /[^A-Za-z0-9]/g;
// using the replace() method to match and remove all the non-alphanumeric
// characters
const newStr = str.replace(regex, '');
console.log(newStr); // BLOCTAKSolutions123
下面的屏幕截图显示了该程序的输出。
以下参数传递给 string.replace
方法。
正斜杠 (/ /
) 标记正则表达式的开始和结束。方括号 []
被称为字符类。
插入符^
符号表示不是以下
。在你的情况下,这意味着在 a-z
和数字 0-9
范围内没有字母。
你将使用 g
(全局)标志来匹配所有包含非字母数字字符的事件,而不仅仅是第一个事件。i
标志使你的匹配不区分大小写 - 你将匹配所有大写和小写字符。
如果你还需要保留空格、连字符或其他字符,请将它们添加到方括号 []
之间。
例如,假设你有一个类似 #BLOCTAKSolutions123?%
的字符串,如下面的 JavaScript 代码所示。
// a string
const str = '#BLOCTAKSolutions123?%';
从上面代码中的字符串可以看出,有一些像 #?%
这样的字符。但是,它们不是数字或字母。
所以在这里,我们将定义一个正则表达式来匹配字符串中的所有非字母数字字符。
// a string
const str = '#BLOCTAKSolutions123?%';
// regex to match all non-alphanumeric characters in string
const regex = /[^A-Za-z0-9]/g;
现在你将使用 replace()
字符串方法并执行以下操作。
- 将
regex
表达式作为第一个参数传递给方法。 - 将一个空字符串
''
作为第二个参数传递给该方法。 - 该方法将返回一个新的
字符串
。
此 JavaScript 代码将从字符串中删除所有非字母数字字符,并将它们替换为空字符串。它将通过以下方式完成,如下面的 JavaScript 代码所示。
const newStr = str.replace(regex, '');
console.log(newStr); // BLOCTAKSolutions
如你所见,newStr
是一个新字符串,但删除了所有非字母数字字符。此代码保留所有字母数字字符、空格和破折号。
你可以通过添加或减去方括号 ([]
) 中的字符来根据需要调整正则表达式。
replace
方法不用于更改原始字符串的内容;它只返回一个新字符串。原因是字符串在 JavaScript 中是不可变的。
因此,在这篇 JavaScript 教程文章的帮助下,你学习了如何使用 JavaScript 中的正则表达式 (RegEx)
从字符串中删除所有非字母数字字符。
你可以使用 str.replace()
方法来实现此目的。str.replace()
方法将返回一个替换所有字符的新字符串。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。