I would like to test a string which should start with any number of digits but not followed by a dot, so I come up these regs, code in jsfiddle:
var startwith = "some string";
reg1 = new RegExp("^" + startWith + "[0-9]+(?!\\.)"),
reg2 = new RegExp("^" + startWith + "\d+(?!\\.)");
var text = "11.1";
console.log(reg1.test(text), reg2.test(text)); // result true, false
I started with reg1, but it fails to return the correct result, so I was just trying the reg2. Surprisingly, the result is correct, but what confuses me is that the two regs return different result, while the patterns are basically equivalent. Anybody have any ideas? all thoughts are appreciated.
new RegExp("^(0-9)+(?!\\.)")\\., but just\d? What is the difference?RegExpconstructor requires double slash so\\dinstead of\dand\\.instead of\.since it accepts a string in constructor. One backslash is for Javascript string and 2nd one is for Javascript regex engine.