Why the test method does not always work correctly - does it show true then false ?
Here, for example, the template /<(.|\n??>/g on tag elements, I look at returning true in the debager, but the condition doesn’t go into the if - it says that it is false , why?
I check this:

var s = "<div></div>"; var pattern = /<(.|\n)*?>/g; if(pattern.test(s)) alert("found"); 
  • I have a globally rendered pattern, but I did a test like this (new RegExp (pattern)). test (s) - and everything is fine, but I can’t understand why if without RegExp it works one time ... - Acne

1 answer 1

The g - global flag means that the regular schedule will be launched several times, and the next time it will be checked from the place where the previous one was completed.

Total we get the following scenario:

 Проверяем строку в первый раз: 1) Сохраняем в регулярке строку 2) Находим и запоминаем точку соответствия 3) Отдаём True Проверяем строку во второй раз: 1) Строка в регулярке соответствует переданной 2) Начинаем поиск с последней точки соответствия 3) Т.к. не находим - удаляем строку из регулярки 4) Отдаём False 
  • everything, thanks understood !!! - Acne