Answer the question
In order to leave comments, you need to log in
js RegExp why not always one result?
I don't understand why using one template and one text does not always produce the same result?
Answer the question
In order to leave comments, you need to log in
RegExp in JavaScript saves its state between calls. Since the expression contains the global match flag g
, the search continues after the next match found.
An instance of a Regexp object has a lastIndex property that allows you to read/write the index at which the next match will start. It will help you figure out:
var RE = /([A-z0-9])/g;
console.log([
RE.lastIndex, RE.test("aaaa"), // 0,true
RE.lastIndex, RE.test("aaaa"), // 1,true
RE.lastIndex, RE.test("aaaa"), // 2,true
RE.lastIndex, RE.test("aaaa"), // 3,true
RE.lastIndex, RE.test("aaaa"), // 4,false
RE.lastIndex, RE.test("aaaa"), // 0,true
]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question