javascript 波斯字母专用正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17315989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:51:39  来源:igfitidea点击:

Dedicated Regular Expression for Persian alphabet

javascriptregex

提问by Angel

How can I define a regular expression which solely accept Persian alphabet characters?

如何定义仅接受波斯字母字符的正则表达式?

I tried the following function, but it doesn't work properly:

我尝试了以下功能,但无法正常工作:

function Just_persian(str){
  var p=/[????????????????????????????????????????????\s]+$/;
  if(!str.match(p))
    alert("invalid format");
}

回答by User 1531343

Persian characters are within the Arabic Unicode block, which ranges from U+0600 to U+06FF (which is specified in character class as \u0600-\u06FF).

波斯语字符在阿拉伯语 Unicode 块中,范围从 U+0600 到 U+06FF(在字符类中指定为\u0600-\u06FF)。

function just_persian(str){
    var p = /^[\u0600-\u06FF\s]+$/;

    if (!p.test(str)) {
        alert("not format");
    }
}

Adapted to JavaScript from this question: Regex for check the input string is just in persian language

从这个问题改编为 JavaScript:Regex for check the input string is just in persian language

回答by Iman Mohamadi

You can use persianRex, it detects all Persian characters in different keyboard layouts and it's open source.

您可以使用persianRex,它检测不同键盘布局中的所有波斯字符,并且它是开源的。

Download it and put it in your project folder. Then include it in your HTML like this:

下载它并将其放在您的项目文件夹中。然后像这样将它包含在你的 HTML 中:

<script src="persian-rex/dist/persian-rex.js"></script>

Then in your Javascript you can do this:

然后在您的 Javascript 中,您可以执行以下操作:

function Just_persian(str){
  if(persianRex.text.test(str))
    alert("not format");
}

回答by AAKousha

Persian Characters are within the range: [\u0600-\u06FF] And: [\s]

波斯字符在范围内:[\u0600-\u06FF] 和:[\s]

Use This Code:

使用此代码:

function Just_persian(str){
  var p=/@"^([\u0600-\u06FF]+\s?)+$"/;
  if(!str.match(p))
    alert("not format");
}

This Patern includes Letter and space Charachters.

此模式包括字母和空格字符。