在正则表达式范围内转义点 [英] Escape dot in a regex range

查看:477
本文介绍了在正则表达式范围内转义点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,这两个正则表达式的行为方式相同:

For some reason those two regex act the same way:

"43\\gf..--.65".replace(/[^\d.-]/g, "");​  // 43..--.65
"43\\gf..--.65".replace(/[^\d\.-]/g, "");​  // 43..--.65

演示

在第一个正则表达式中,我没有逃避点(),而在我做的第二个正则表达式中( \。)。

In the first regex I don't escape the dot(.) while in the second regex I do(\.).

有什么区别以及为什么它们的行为相同?

What are the differences and why they act the same?

推荐答案

因为点在字符类内(方括号 [] )。

Because the dot is inside character class (square brackets []).

查看 http: //www.regular-expressions.info/reference.html ,它说(在char类部分下):

Take a look at http://www.regular-expressions.info/reference.html, it says (under char class section):


任何字符除了^ - ] \将该字符添加到字符类的可能匹配

Any character except ^-]\ add that character to the possible matches for the character class.

这篇关于在正则表达式范围内转义点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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