I wrote a regex to weed out bad calculator inputs (disallow consecutive decimal points, no decimal followed by an operation, i.e., +.1, etc.). So, the regex looks like this:
const regex = /\.[+-/x]|\.[0-9]+\.| \.+|[+-/x]\./
And it works - anything that matches this is weeded out. My problem is, I don't know why it works. Also, this doesn't work, for some reason, if I delete the space between the second-to-last "OR" operator and the next escape character. Can someone explain this? Also, is there a simpler, equivalent, regular expression I could use? This expression results in the following inputs/outputs:
!regex.test('3.33') // true !regex.test('3.33.3') // false !regex.test('0.333') // true !regex.test('+.') // false !regex.test('.-') // false
https://stackoverflow.com/questions/66610238/having-trouble-understanding-regular-expressions-in-javascript March 13, 2021 at 12:49PM
没有评论:
发表评论