0

I have given a regex

let reg = new RegExp("^[-]?[0-9]*.?[0-9]{1,4}$");
console.log(reg.test("45-45")); //true
console.log(reg.test("sd545")); //true
console.log(reg.test("5*45")); //true

Expected behavior if any string or speial char it should through error

the above all console should through false What i am doing wrong?

Mohan Gopi
  • 7,606
  • 17
  • 66
  • 117
  • `.` matches any char but a line break char. You must escape it with double backslashes in the RegExp constructor. Make sure you pass the right test *strings* to the `RegExp#test()` method. Note that `console.log(reg.test("sd545"))` is *false*, not *true*. – Wiktor Stribiżew Jul 12 '19 at 10:18
  • `reg.test(45-45)` is literally `reg.test(0)`. `reg.test(sd545)` - no idea what `sd545` contains. `reg.test(5*45)` = `reg.test(225)` – VLAZ Jul 12 '19 at 10:20
  • please check my updated question and "sd545" is the value which i gave for test i am expecting it to through false but it is providing true – Mohan Gopi Jul 12 '19 at 10:24
  • @VLAZ i think the issue is in my regex – Mohan Gopi Jul 12 '19 at 10:36

0 Answers0