Why output of my Javascript regex is inconsistent? -


this question has answer here:

in browser console output of following function series of alterante true , false. idea?

      function checkspecificlanguage(text_val) {     // force string type`enter code here`           var regex =   /^[\u3000-\u303f\u3040-\u309f\u30a0-\u30ff\uff00-\uffef\u4e00-\u9faf\u2605-\u2606\u2190-\u2195u203b]+$/ig;           console.log( text_val+"-"+regex.test(text_val) );         console.log( text_val+"-"+regex.test(text_val) );         console.log( text_val+"-"+regex.test(text_val) );         console.log( text_val+"-"+regex.test(text_val) );         console.log( text_val+"-"+regex.test(text_val) );         return  regex.test(text_val);     }  checkspecificlanguage("でしたコンサート");   

you're using global (g) flag.

according mdn:

as exec() (or in combination it), test() called multiple times on same global regular expression instance advance past previous match.

you can around setting lastindex.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -