国际化的正则表达式

UPDATE: There's more on Internationalized RegExs in this StackOverflow question.

更新:这个StackOverflow问题中有更多关于国际化RegEx的信息。

I was trying to make a regular expression for use in client-side JavaScript (using a PeterBlum Validator) that allowed a series of special characters:

我试图制作一个正则表达式以用于客户端JavaScript (使用PeterBlum Validator ),该表达式允许一系列特殊字符:

-'.,&#@:?!()$\/

-'。,&#@:?!()$ \ /

Plus letters and numbers and whitespace:

加上字母,数字和空格:

\w\d\s

\ w \ d \ s

However, I mistakenly assumed that \w meant truly "word characters." It doesn't, it means [A-Za-z].

但是,我错误地认为\ w的意思是真正的“单词字符”。 不是,意思是[A-Za-z]。

That sucks. What about José, when he wants to put his First Name into a form?

糟透了。 如果何塞想将其名字输入表格,该怎么办?

Well, I could do a RegEx that denies specific characters and allows all others, but I really just wanted to support Spanish, French, English, German, and any language that uses the general Latin Character Set.

好吧,我可以做一个RegEx来拒绝特定字符并允许所有其他字符,但是我真的只想支持西班牙语,法语,英语,德语以及使用常规拉丁字符集的任何语言。

So, here's what I have.

所以,这就是我所拥有的。

^[  ÀÈÌÒÙ àèìòù ÁÉÍÓÚ Ý áéíóúý   ÂÊÎÔÛ âêîôû ÃÑÕ ãñõ ÄËÏÖÜŸ   äëïöüŸ ¡¿çÇŒœ ßØøÅå ÆæÞþ  Ðð ""\w\d\s-'.,&#@:?!()$\/]+$

^ [ÁÈÌÒÙàèìòùÁÉÍÓÚÝééóúýÊÎÔÛâêîôûÃÑÕõñõÄËÏÖÜŸäëïööŸ¡

Did I miss anything? (Ignore the  whitespace for the purposes of this post's RegEx)

我有想念吗? (出于本文的RegEx的目的,请忽略空格)

It's lame that \w doesn't work on the client-side based on your browser's locale. This makes it difficult for your RegExes to have parity between the client and server.

根据浏览器的语言环境,\ w在客户端不起作用是很遗憾的。 这使RegExes很难在客户端和服务器之间具有奇偶校验。

翻译自: https://www.hanselman.com/blog/internationalized-regular-expressions

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值