PHP正则表达式和JavaScript正则表达式之间的区别 [英] Difference between PHP regex and JavaScript regex
问题描述
您好我想在spry java脚本框架中使用下面的php正则表达式,但它们不能用于spry框架,spry不会让用户输入!。$
1)/ ^ [\d] + $ /
2)/ ^([\ x {600} -\ x {6FF}] + \ s)* [\ x {600} -\ x {6FF}] + $ / u
3)/ ^([{\x 600} {-\x} 6FF] + \d * \s)* [{\x 600} {-\x} 6FF] + \ d * $ / u
请帮我转换它们在spry框架中使用。
Hi i want to use below php regex in spry java script framework but them doesn't work with spry framework and spry doesn't let the user to input!.
1)"/^[\d]+$/"
2)"/^([\x{600}-\x{6FF}]+\s)*[\x{600}-\x{6FF}]+$/u"
3)"/^([\x{600}-\x{6FF}]+\d*\s)*[\x{600}-\x{6FF}]+\d*$/u"
please help me to convert them to use in spry framework.
推荐答案
1) /^[\d]+$/
2) /^([\u0600-\u06FF]+\s)*[\u0600-\u06FF]+$/
3) /^([\u0600-\u06FF]+\d*\s)*[\u0600-\u06FF]+\d*$/
/ u不受支持,因为Javascript正则表达式仅支持unicode代码点条款。 \ x {???}(unicode codepoints)应写成\u ????在Javascript正则表达式中(总是4位0填充)
/u is not supported, since Javascript regexes only supports unicode in terms of codepoints. \x{???} (unicode codepoints) should be written \u???? in Javascript regex (always 4 digits 0 padded)
在这些情况下,以下内容适用于正则表达式的其余部分:
In these cases, the following applies to the rest of the regex:
- \s在Javascript中被视为unicode
- \d不是,这意味着只接受ASCII数字(0-9) 。
这意味着我们必须特别允许外国数字,例如波斯语(代码点06F0-06F9):
This means we specifically have to allow "foreign" numerals, e.g. Persian (codepoints 06F0-06F9):
1) /^[\d\u06F0-\u06F9]+$/
2) /^([\u0600-\u06FF]+\s)*[\u0600-\u06FF]+$/
3) /^([\u0600-\u06FF]+[\d\u06F0-\u06F9]*\s)*[\u0600-\u06FF]+[\d\u06F0-\u06F9]*$/
(如果不接受ASCII数字,则删除\d)
(Remove \d if ASCII digits shouldn't be accepted)
不确定括号在示例1中应该做什么,最初可以写成:
Not sure what the brackets are supposed to be doing in example 1, originally they could be written:
1) /^\d+$/
但要添加波斯数字,我们需要它们,见上文。
But to add the Persian numerals, we need them, see above.
更新
然而,Spry字符屏蔽只需要一个正则表达式应用于每个输入的字符 - 即,我们实际上不能进行模式匹配,它只是所有位置中接受字符的列表,在这种情况下:
Spry character masking, however, only wants a regex to be applied on each entered character - i.e., we can't actually do pattern matching, it's just a "list" of accepted characters in all places, in which case:
1 ) /[\u06F0-\u06F9\d]/ // match 0-9 and Persian numerals
2 and 3) /[\u0600-\u06FF\d\s]/ // match all Persian characters (includes numerals), 0-9 and space
如果您不想接受,请再次删除\d 0-9。
Once again, remove \d if you don't want to accept 0-9.
更新2
现在...使用正则表达式使用Spry 验证:
Now... using regex for validation with Spry:
var checkFullName = function(value, options)
{
// Match with the by now well-known regex:
if (value.match(/^([\u0600-\u06FF]+\s)*[\u0600-\u06FF]+$/))
{
return true;
}
return false;
}
var sprytextfield =
new Spry.Widget.ValidationTextField(
"sprytextfield",
"custom",
{ validation: checkFullName, validateOn: ["blur", "change"] }
);
可以为案例3制作类似的自定义函数。
A similar custom function can be made for case 3.
请参阅 Adobe实验室的示例
这篇关于PHP正则表达式和JavaScript正则表达式之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!