繁体   English   中英

regexp 允许中文或字母字符

[英]regexp allow chinese or alphabetic characters

  • 我试图允许输入中文或字母字符。

     var name="TEXT" //here input either alphabetic or Chinese Characters 请输入let reqChinesePos=/^[\ \㐀-\䶿\一-\鿿]\\d{8}$/; let reqEnglish=/^[A-Za-z]\\d{40}$/ console.log(reqEnglish.test(name)); //Here true but here also Chinese characters also match. console.log(reqChinesePos.test(name)); //Here true but here also alphabetic characters also match.

预期结果 :

console.log(reqEnglish.test(name));//here only allow alphabetic characters not allow chinese.
console.log(reqChinesePos.test(name));//here only allow Chinese characters not allow English.

您的汉字字符类范围似乎是正确的,至少从这里的简单本地测试来看是这样。 但是,我发现这两种模式都有另一个问题,如果您想在两种情况下都允许使用罗马数字,那么\\d可能应该是字符类的一部分。 进行此更改后,然后为输入提供一个宽度或宽度范围。 假设你想要宽度都为 8,你可以尝试:

 var reqChinesePos = /^[\ \㐀-\䶿\一-\鿿\\d]{8}$/; var reqEnglish = /^[A-Za-z\\d]{8}$/ var name1 = "大猫大猫大猫大猫"; var name2 = "JONATHAN"; var name3 = "BIGCAT大猫"; console.log(name1); console.log(reqEnglish.test(name1)); console.log(reqChinesePos.test(name1)); console.log(name2); console.log(reqEnglish.test(name2)); console.log(reqChinesePos.test(name2)); console.log(name3); console.log(reqEnglish.test(name3)); console.log(reqChinesePos.test(name3));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM