0

Can someone explain to me what the following regex is doing in javascript? I know it only allows for two decimal places but I'm very confused on the multiple "\d" and "." and "\^d." expressions.

Regex expression: this.value = this.value.replace (/(.\d\d)\d+|([\d.]*)[^\d.]/, '$1$2');

Original StackOverflow question here: Allow only 2 decimal points entry to a textbox using javascript or jquery?

Community
  • 1
  • 1
M Jenkins
  • 25
  • 5
  • This regex makes no sence. Where did you get it? –  Apr 05 '17 at 16:41
  • regex101.com explains regex expressions. Here is a link to an explanation of your regex (look at the right side of the screen): https://regex101.com/r/GLtNXX/1 – Nelson Teixeira Apr 05 '17 at 16:50
  • The $1 and $2 it's the contents of the 2 capture groups of the expression 1st: (.\d\d) 2nd: ([\d.]*). As you're using | it can capture one or the other, which are then mounted in the replace string. – Nelson Teixeira Apr 05 '17 at 16:54

0 Answers0