samedi 3 août 2019

RegExt test returns positive and negative for same string

I am using regex test to test string and it returns "true" and "false" for same string. First time I run it, returns "true", second time returns "false". Why is that? My code is:

    const patterns = /^[a-zšđčćž ]{3,30}$/gi;

    async  function validate(field, regex){
    return regex.test(field);
    }

    async function fetchSearch(){
        const mystring = "Perfect text to test"; 
        var resp = await validate(mystring, patterns)
        .then( function(response){  
            if(response){ 
                console.log("Response is positive: " + mystring);           
                } else { 
                console.log("Response is negative: " + mystring);

            } 
        });  

    }

If I run it for 3 times, I get this in console:

JS: Response is positive: Perfect text to test
JS: Response is negative: Perfect text to test
JS: Response is positive: Perfect text to test

But why is that?

Aucun commentaire:

Enregistrer un commentaire