JavaScript 正则表达式校验字符串语种 (中、英、日、韩)
"使用 JavaScript 正则表达式校验字符串是否只包含中文、英文、日文和韩文。以下是一个示例的正则表达式:\n\n\n/^[一-龥ぁ-ゔァ-ヴー々〆〤\u3130-\u318F\uAC00-\uD7A3]+$/g\n\n\n这个正则表达式可以校验字符串是否只包含中文、日文、韩文以及部分英文字符。如果字符串中包含其他字符,校验将失败。\n\n使用这个正则表达式可以通过 JavaScript 的 test() 方法来校验字符串,例如:\n\njavascript\nconst regex = /^[一-龥ぁ-ゔァ-ヴー々〆〤\u3130-\u318F\uAC00-\uD7A3]+$/g;\n\nconst str1 = '你好'; // 只包含中文,校验成功\nconsole.log(regex.test(str1)); // 输出: true\n\nconst str2 = 'Hello'; // 包含英文,校验失败\nconsole.log(regex.test(str2)); // 输出: false\n\nconst str3 = '안녕하세요'; // 只包含韩文,校验成功\nconsole.log(regex.test(str3)); // 输出: true\n\nconst str4 = 'こんにちは'; // 只包含日文,校验成功\nconsole.log(regex.test(str4)); // 输出: true\n\nconst str5 = '你好Hello안녕하세요こんにちは'; // 包含中文、英文、韩文和日文,校验失败\nconsole.log(regex.test(str5)); // 输出: false\n\n\n注意,这个正则表达式只能判断字符串是否只包含中、英、日、韩的语种,而不能判断字符串是否包含全部这些语种。如果需要判断字符串是否包含全部这些语种,可以使用其他的方法,比如利用 Unicode 范围来判断。
原文地址: https://www.cveoy.top/t/topic/qgE1 著作权归作者所有。请勿转载和采集!