简体   繁体   中英

regexp allow chinese or alphabetic characters

  • I tried to allow input either Chinese or Alphabetic Character.

     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.

Expected result :

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.

Your character class range for Chinese characters seems correct, at least from simple local testing here. But, I see another problem with both patterns, in that the \\d should probably be part of the character class, if you want to also allow Roman numerals in both cases. After making this change, then give a width or range of widths for the input. Assuming you wanted a width of 8 both, you could try:

 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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