如何使regex匹配整个字符串而不是单个字符

时间:2022-10-14 20:13:09

I am trying to implement "alpha" validation on Arabic alphabet characters input, using the JavaScript regex /[\u0600-\u06FF]/ as instructed in this post. I want to accept only Arabic alphabet characters and spaces.

我正在尝试使用JavaScript regex /[\u0600-\u06FF]/按照本文的指示,对阿拉伯字母字符输入实现“alpha”验证。我只接受阿拉伯字母字符和空格。

Now the problem is it gives the following result:

现在的问题是它给出了以下结果:

r = /[\u0600-\u06FF]/

r.test("abcd")      // false - correct
r.test("@#$%^")     // false - correct
r.test("س")         // true  - correct
r.test("abcd$$#5س") // true  - should be false
r.test("abcdس")     // true  - should be false

If a single matching character is given, then it is classifying the whole input as acceptable, even if the rest of the input is full of unacceptable chars. What regex should I be using instead?

如果给定了一个匹配字符,那么它将把整个输入分类为可接受的,即使其余的输入都是不可接受的字符。我应该使用什么regex ?

2 个解决方案

#1


3  

You need to add ^ and $ anchors to the regular expression, as well as a + to allow multiple characters.

您需要添加^和$锚正则表达式,以及+允许多个字符。

Try this:

试试这个:

/^[\u0600-\u06FF]+$/

I'm not sure if "Arabic spaces" that you mentioned are included in the character range there, but if you want to allow white space in the string then just add a \s inside the [] brackets.

我不确定您提到的“阿拉伯空格”是否包含在字符范围中,但是如果您想在字符串中允许空格,那么只需在[]括号中添加一个\s。

#2


0  

You can explicitly allow some keys e-g: numpad, backspace and space, please check the code snippet below:

您可以显式地允许一些键e-g: numpad、backspace和space,请查看下面的代码片段:

function restrictInputOtherThanArabic($field)
{
  // Arabic characters fall in the Unicode range 0600 - 06FF
  var arabicCharUnicodeRange = /[\u0600-\u06FF]/;

  $field.bind("keypress", function(event)
  {
    var key = event.which;

    // 0 = numpad
    // 8 = backspace
    // 32 = space
    if (key==8 || key==0 || key === 32)
    {
      return true;
    }

    var str = String.fromCharCode(key);
    if ( arabicCharUnicodeRange.test(str) )
    {
      return true;
    }

    return false;
  });
}

// call this function on a field
restrictInputOtherThanArabic($('#firstnameAr'));

#1


3  

You need to add ^ and $ anchors to the regular expression, as well as a + to allow multiple characters.

您需要添加^和$锚正则表达式,以及+允许多个字符。

Try this:

试试这个:

/^[\u0600-\u06FF]+$/

I'm not sure if "Arabic spaces" that you mentioned are included in the character range there, but if you want to allow white space in the string then just add a \s inside the [] brackets.

我不确定您提到的“阿拉伯空格”是否包含在字符范围中,但是如果您想在字符串中允许空格,那么只需在[]括号中添加一个\s。

#2


0  

You can explicitly allow some keys e-g: numpad, backspace and space, please check the code snippet below:

您可以显式地允许一些键e-g: numpad、backspace和space,请查看下面的代码片段:

function restrictInputOtherThanArabic($field)
{
  // Arabic characters fall in the Unicode range 0600 - 06FF
  var arabicCharUnicodeRange = /[\u0600-\u06FF]/;

  $field.bind("keypress", function(event)
  {
    var key = event.which;

    // 0 = numpad
    // 8 = backspace
    // 32 = space
    if (key==8 || key==0 || key === 32)
    {
      return true;
    }

    var str = String.fromCharCode(key);
    if ( arabicCharUnicodeRange.test(str) )
    {
      return true;
    }

    return false;
  });
}

// call this function on a field
restrictInputOtherThanArabic($('#firstnameAr'));