regex - JavaScript Regular Expression Match Return the Input as the First 2 Indexies -
i getting started regex in javascript (and in general) , have come across oddity can't seem find explanation using google.
without getting detail, using following expression:
/(^sw:\s?(-?\d{1,3}.\d{1,6})\sne:\s?(-?\d{1,3}.\d{1,6})$)/i
which matching user input like:
sw: -27.990344 ne: 150.234562
with following console.log output:
["sw: -27.345455 ne: 180.234567", "sw: -27.345455 ne: 180.234567", "-27.345455", "180.234567"] 0 "sw: -27.345455 ne: 180.234567" 1 "sw: -27.345455 ne: 180.234567" 2 "-27.345455" 3 "180.234567" index 0 input "sw: -27.345455 ne: 180.234567"
my question is: why index 0 , 1 returning user supplied input , actual data return index 2 , 3?
i assume relative expression, don't know enough sure not normal behaviour.
any appreciated.
index 0 complete match, capturing groups starting @ index 1.
the capturing groups numbered opening brackets, first opening bracket group 1, second opening bracket group 2, ...
you have brackets around complete expression, first capturing group. ==> outer brackets not needed, can remove them , groups start @ index 1.
for more information capturing groups can have @ /www.regular-expressions.info, resource regexes.
Comments
Post a Comment