韩文和英文字母正则表达式返回false [英] korean and alphabet regex returning false

查看:194
本文介绍了韩文和英文字母正则表达式返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直无法验证我的网站的表单输入。我在接受英文字符和韩文字符的名称字段。我没有输入韩文字符的麻烦,但在我加入韩文正则表达式后,函数返回false,而没有返回true。

  var recommend_coffee_your_name_field = $('#coffee_recommend_your_name_field'); 

recommend_coffee_your_name_field.keyup(函数(e){
var $ th = $(this);
$ th.val($ th.val()。replace(/ ^ a-zA-Z0-9\\u {3130} -\\\ {318F} \u {AC00} -\u {D7AF}] / g,函数(str){
alert(' ')+'');

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;
}
i f(!recommend_coffee_shop_your_name_value){
alert(名称字段丢失);
e.preventDefault();
}
});


解决方案

在您的情况下,来自Unicode转义序列的大括号:

  / [^ a-zA-Z0-9\\\㄰-\\\㆏\ uAC00- \\\\\\\\\\\\\\\\\\\\\\\\\\\\\'''' } 只能在兼容ECMAScript 6的浏览器中使用,请参阅 ECMAScript 6兼容性表



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


Unicode转义序列在 \u 之后至少需要四个字符。

示例 \\\© = ©


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


ECMAScript 6.使用Unicode代码点转义,可以使用十六进制数字转义任何字符,以便可以使用高达 0x10FFFF 的Unicode代码点。通过简单的Unicode转义,通常需要单独编写代理两半以实现相同目的。 示例: \\ \\ u {2F804} //与简单的Unicode转义相同: \\\�\\\�


< blockquote>

I have been having trouble validating a form input for my website. Im looking the accept both english characters and korean characters for the name field. I had no trouble with inputting korean characters but at soon as I added the korean regex, the function has been returning false while without it returns 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();
    } 
});

解决方案

In your case, it is enough to remove the curly braces from the Unicode escape sequences:

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

Note that \u{3130} is only possible in ECMAScript 6 compatible browsers, please refer to ECMAScript 6 Compatibility Table.

The JavaScript String Literals Guide calls \uXXXX Unicode escape sequences:

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

And \u{XXXXX} are called Unicode code point escapes:

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

这篇关于韩文和英文字母正则表达式返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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