Firebase rules regex troubles

混江龙づ霸主 提交于 2019-12-02 04:03:06

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