Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

inconsistancy in regex

Can anyone explain this to me? Why does "GETTING STARTED" match a[^cns] until I put it into an H1? (Testing on Chrome)

        var regex = /a[^cns]/gim
        console.log("test: " + regex.test("GETTING STARTED"));
        var h = document.createElement("H1");
        h.innerText = "GETTING STARTED";
        console.log(regex.test(h.innerText));

If I change this to

    var regex = /a[^cns]/gim
    console.log("test: " + regex.test("GETTING STARTED"));
    regex.lastIndex = 0;
    var h = document.createElement("H1");
    h.innerText = "GETTING STARTED";
    console.log(regex.test(h.innerText));

It works as expected

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Don’t use //g.test()

Using test() on a regex with the "global" flag

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading