为什么字符范围类 [A-z] 匹配下划线? [英] Why does character range class [A-z] match underscore?

查看:49
本文介绍了为什么字符范围类 [A-z] 匹配下划线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在多种语言中尝试过,结果相同.

JavaScript 示例:

/[A-z]/.test("_");//=>真的/[A-z]/.test("0");//=>错误的/[A-z]/.test("-");//=>错误的/[A-z]/.test("A");//=>真的

为什么第一种情况不返回false?

解决方案

字符范围不是那么智能.它们基于 ASCII 码.查看 Ascii 表.大写A-Z和小写a-z范围之间存在特殊字符,即:

<预><代码>[\]^_`

所以,应该是 A-Za-z 而不是 A-z.

Tried this in many languages with same result.

JavaScript example:

/[A-z]/.test("_"); // => true
/[A-z]/.test("0"); // => false
/[A-z]/.test("-"); // => false
/[A-z]/.test("A"); // => true

Why is the first case not returing false?

解决方案

Character ranges are not that intelligent. They are based on ascii codes. Check out Ascii Table. There exist special characters between upper-case A-Z and lower-case a-z range, namely:

[
\
]
^
_
`

So, instead of A-z it should be A-Za-z.

这篇关于为什么字符范围类 [A-z] 匹配下划线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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