Bất cứ ai cũng có thể giải thích cho tôi, tại sao địa chỉ Regex biến và không địa phương Biến Regex có đầu ra khác nhau.Regex.test() là cho đúng tuần tự sai?
var regex1 = /a|b/g;
function isAB1() {
return regex1.test('a');
}
console.log(isAB1()); // true
console.log(isAB1()); // false
console.log(isAB1()); // true
console.log(isAB1()); // false
function isAB2() {
var regex2 = /a|b/g;
return regex2.test('a');
}
console.log(isAB2()); // true
console.log(isAB2()); // true
console.log(isAB2()); // true
console.log(isAB2()); // true
Tôi đã tạo JSFiddle
cho cùng một here.
từ [doc] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test): 'được gọi nhiều lần trên cùng một cá thể biểu thức chính quy toàn cầu sẽ trước qua trận đấu trước đó'. – Hacketo