Firebase规则正则表达式的麻烦 [英] Firebase rules regex troubles
问题描述
^([-+]?)([\d]{1,2})(((\.)(\d+)(,)))(([-+]?)([\d]{1,3})((\.)(\d+))?)$
我正在尝试使用上述正则表达式来确认数据是否为有效坐标。我很难让它与Firebase规则一起使用。当我在在线正则表达式测试仪中运行正则表达式时,它可以正常运行,但是Firebase规则似乎不接受它。
I am trying to use this regex above to be able to confirm that the data is a valid coordinate. I am having trouble getting this to work with Firebase rules. When i run the regex in an online regex tester, it works okay, but Firebase rules doesn't seem to accept it.
这是我的Firebase规则:
Here is my firebase rule:
".validate": "newData.isString() && newData.val().matches(/^([-+]?)([\d]{1,2})(((\.)(\d+)(,)))(([-+]?)([\d]{1,3})((\.)(\d+))?)$/)"
反正有办法使它正常工作吗?
Is there anyway to get this working?
推荐答案
您需要将转义的反斜杠加倍,但说实话,您的表达式包含太多冗余分组构造。
You need to double the escaping backslashes, but honestly, your expression contains too many redundant grouping constructs.
使用
.matches(/^[-+]?\\d{1,2}\\.\\d+,[-+]?\\d{1,3}(\\.\\d+)?$/)
或完全避免反斜杠:
.matches(/^[-+]?[0-9]{1,2}[.][0-9]+,[-+]?[0-9]{1,3}([.][0-9]+)?$/)
正则表达式将匹配字符串像在此在线演示中一样。
The regex will match strings like in this online demo.
详细信息:
-
^
-开始字符串(在 Firebase正则表达式中,当在 -
[-+]?
-1或0+
或-
-
[0-9] {1,2}
-1或2位数字 -
[。]
-点 -
[0-9] +
-1个以上数字 -
,
-逗号 -
[-+]?
-1或0+
或-
-
[0-9] {1,3}
-1至3位数字 -
([。] [0-9] +)?
-1或0个序列。 / code>和1个以上的数字(请注意,不支持非捕获组)
-
$
-字符串结尾锚(仅在模式结尾处,$
与Firebase正则表达式中的字符串结尾匹配)。
^
- start of string (in Firebase regex, it is an anchor when used at the start of the pattern only)[-+]?
- 1 or 0+
or-
[0-9]{1,2}
- 1 or 2 digits[.]
- a dot[0-9]+
- 1+ digits,
- a comma[-+]?
- 1 or 0+
or-
[0-9]{1,3}
- 1 to 3 digits([.][0-9]+)?
- 1 or 0 sequences of.
and 1+ digits (note that non-capturing groups are not supported)$
- end of string anchor (only when at the pattern end,$
matches the end of string in Firebase regex).
这篇关于Firebase规则正则表达式的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!