Firebase rules regex troubles

空扰寡人 提交于 2019-12-20 04:32:37

问题


^([-+]?)([\d]{1,2})(((\.)(\d+)(,)))(([-+]?)([\d]{1,3})((\.)(\d+))?)$

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.

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?


回答1:


You need to double the escaping backslashes, but honestly, your expression contains too many redundant grouping constructs.

Use

.matches(/^[-+]?\\d{1,2}\\.\\d+,[-+]?\\d{1,3}(\\.\\d+)?$/)

or avoid the backslashes altogether:

.matches(/^[-+]?[0-9]{1,2}[.][0-9]+,[-+]?[0-9]{1,3}([.][0-9]+)?$/)

The regex will match strings like in this online demo.

Details:

  • ^ - 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).


来源:https://stackoverflow.com/questions/44220860/firebase-rules-regex-troubles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!