^([-+]?)([\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?
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).