You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When you use a regex with these flags, the .test() method internally updates the lastIndex property of the RegExp object. The lastIndex is where the next search starts. After the first execution of .test(), the lastIndex is updated to the end of the last match. So, the next time .test() is invoked, it continues from the lastIndex.
In the code:
The regular expression /[\uFF00-\uFFEF]/g matches Unicode characters in the range FF00 to FFEF.
With the first call regexp.test(',') (where ',' is the Unicode character FF0C), it matches in this range, so the method returns true, and the lastIndex property of the regex is set to the end of the string.
On the second call regexp.test(','), the search starts from the end of the string (as per lastIndex), so there isn't anything to match, hence it returns false and resets lastIndex back to 0 for the next search.
Therefore, to continually get true from the .test() method, you have to reset the lastIndex manually every time after using the .test() method.
Here is a modification of the code that should work as intended:
When you use a regex with these flags, the
.test()
method internally updates thelastIndex
property of the RegExp object. ThelastIndex
is where the next search starts. After the first execution of.test()
, thelastIndex
is updated to the end of the last match. So, the next time.test()
is invoked, it continues from thelastIndex
.In the code:
/[\uFF00-\uFFEF]/g
matches Unicode characters in the range FF00 to FFEF.regexp.test(',')
(where ',' is the Unicode character FF0C), it matches in this range, so the method returnstrue
, and thelastIndex
property of the regex is set to the end of the string.regexp.test(',')
, the search starts from the end of the string (as perlastIndex
), so there isn't anything to match, hence it returnsfalse
and resetslastIndex
back to 0 for the next search.Therefore, to continually get
true
from the.test()
method, you have to reset thelastIndex
manually every time after using the .test() method.Here is a modification of the code that should work as intended:
The text was updated successfully, but these errors were encountered: