用于 Javascript 的 RegEx 仅允许字母数字 [英] RegEx for Javascript to allow only alphanumeric

查看:34
本文介绍了用于 Javascript 的 RegEx 仅允许字母数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到一个只允许字母数字的正则表达式.到目前为止,我尝试的每个人都只在字符串是字母数字时才有效,意思是包含一个字母和一个数字.我只想要一个允许两者之一而不需要两者.

I need to find a reg ex that only allows alphanumeric. So far, everyone I try only works if the string is alphanumeric, meaning contains both a letter and a number. I just want one what would allow either and not require both.

推荐答案

/^[a-z0-9]+$/i

^         Start of string
[a-z0-9]  a or b or c or ... z or 0 or 1 or ... 9
+         one or more times (change to * to allow empty string)
$         end of string    
/i        case-insensitive

更新(支持通用字符)

如果您需要此正则表达式支持通用字符,您可以在此处找到Unicode 字符列表.

if you need to this regexp supports universal character you can find list of unicode characters here.

例如:/^([a-zA-Z0-9u0600-u06FFu0660-u0669u06F0-u06F9 _.-]+)$/

这将支持波斯语.

这篇关于用于 Javascript 的 RegEx 仅允许字母数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆