Skip to content

Latest commit

 

History

History
34 lines (22 loc) · 1.16 KB

is-alpha-numeric.md

File metadata and controls

34 lines (22 loc) · 1.16 KB
标题 标签
isAlphaNumeric(判断字符是否只包含字母和数字) string,regexp(字符串,正则表达式)

检查字符串是否仅包含字母数字字符。

  • 使用 RegExp.prototype.test() 检查输入字符串是否与字母数字正则表达式模式匹配。
const isAlphaNumeric = str => /^[a-z0-9]+$/gi.test(str);

ts 代码如下:

调用方式:

isAlphaNumeric('hello123'); // true
isAlphaNumeric('123'); // true
isAlphaNumeric('hello 123'); // false (space character is not alphanumeric)
isAlphaNumeric('#$hello'); // false

应用场景

以下是一个实战示例:

结果如下:

<iframe src="codes/javascript/html/is-alpha-numeric.html"></iframe>