允许来自任何语言的字母(例如“ñ")的正则表达式 [英] Regular expression that allows letters (like "ñ") from any language

查看:115
本文介绍了允许来自任何语言的字母(例如“ñ")的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图让用户使用其他语言的特殊字符,例如西班牙语或法语.我原来有这个:

trying to let users use special characters in other languages such as Spanish or French. I originally had this:

 "/[^A-Za-z0-9\.\_\- ]/i" 

,然后将其更改为

 "/[^\p{L}\p{N}\.\_\-\(\) ]/i" 

,但仍然无法正常工作.应允许使用诸如ñ"之类的字母.谢谢.

but still doesn't work. letters such as "ñ" should be allowed. Thanks.

修订: 我发现在开头添加(* UTF8)有助于解决问题.因此,我使用以下代码:"/(* UTF8)[^ \ p {L} A-Za-z0-9 ._-]/i"

Revision: I found that adding a (*UTF8) at the beginning helps solve the problem. So I'm using the following code:"/(*UTF8)[^\p{L}A-Za-z0-9._- ]/i"

修订: 在查看了答案之后,我决定使用:"/[^ \ p {Xwd}.-]/u".谢谢(它甚至适用于中文字母.

Revision: After looking at the answers I decided to use: "/[^\p{Xwd}. -]/u". Thanks(It works even with the Chinese alphabet.

推荐答案

对于拉丁语言,您可以使用\p{Latin}字符类:

for latin languages you can use the \p{Latin} character class:

/[^\p{Latin}0-9._ -]/u

但是,如果您需要所有其他字母和数字:

But if you want all other letters and digits:

/[^\p{Xwd}. -]/u

"u"修饰符表示该字符串必须作为unicode字符串读取.

The "u" modifier indicates that the string must be read as an unicode string.

这篇关于允许来自任何语言的字母(例如“ñ")的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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