gpt4 book ai didi

javascript - 韩文和字母正则表达式返回 false

转载 作者:行者123 更新时间:2023-11-29 10:11:44 24 4
gpt4 key购买 nike

我一直无法验证我网站的表单输入。我正在寻找名称字段接受英文字符和韩文字符。我在输入韩文字符时没有遇到任何问题,但在我添加韩文正则表达式后,该函数一直返回 false,而没有返回 true。

var recommend_coffee_your_name_field = $('#coffee_recommend_your_name_field');

recommend_coffee_your_name_field.keyup(function(e) {
var $th = $(this);
$th.val( $th.val().replace(/[^a-zA-Z0-9\u{3130}-\u{318F}\u{AC00}-\u{D7AF}]/g, function(str) {
alert('You typed " ' + str + ' ".\n\nPlease use only letters and numbers.'); return '';
} ) );

if(e.keyCode !== 8 || e.keyCode !== 9 ) {
var recommend_coffee_your_name_field_length = recommend_coffee_your_name_field.val().length;
//console.log("Your Name Field Length: " + your_name_field_length);

if( recommend_coffee_your_name_field_length > 0 ){
//recommend_coffee_your_name_field.removeClass("error").addClass("success");
recommend_coffee_shop_your_name_value = true;
}
else{
//recommend_coffee_your_name_field.removeClass("success").addClass("error");
recommend_coffee_shop_your_name_value = false;
}
if (!recommend_coffee_shop_your_name_value) {
alert("The name field is missing");
e.preventDefault();
}
});

最佳答案

在您的情况下,从 Unicode 转义序列中删除大括号就足够了:

/[^a-zA-Z0-9\u3130-\u318F\uAC00-\uD7AF]/g

请注意,\u{3130} 仅适用于 ECMAScript 6 兼容浏览器,请参阅 ECMAScript 6 Compatibility Table .

JavaScript String Literals Guide调用 \uXXXX Unicode 转义序列:

The Unicode escape sequences require at least four characters following \u.
Example: \u00A9 = ©

\u{XXXXX}被称为Unicode代码点转义:

New in ECMAScript 6. With Unicode code point escapes, any character can be escaped using hexadecimal numbers so that it is possible to use Unicode code points up to 0x10FFFF. With simple Unicode escapes it is often necessary to write the surrogate halves separately to achieve the same.

Example:\u{2F804} // The same with simple Unicode escapes: \uD87E\uDC04

关于javascript - 韩文和字母正则表达式返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32239102/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com