Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Regular Expression is working fine first time but not second time, working again for 3rd time and not for 4th and so on

Javascript Regular Expression is working fine first time but not second time, working again for 3rd time and not for 4th and so on :(

Script :

<script language="javascript" type="text/javascript">
    var reg = /[^\w]/gi;
    function checkNonWordChars() {
        var str = $("#TestTextbox").val();
        if (reg.test(str)) {
            alert('!!! Non-Word Char Exists !!!');
        }
        else {
            alert('input accepted');
        }
    }    
</script>

HTML :

<input type="text" id="TestTextbox"  />
<input type="button" value="Test" onclick="checkNonWordChars();" />

If I click on button once, it will fire an alert saying that “!!! Non-Word Char Exists !!!” but if I click it again, it will fire an alert saying “input accepted” :(

like image 536
Nilesh Thakkar Avatar asked Nov 05 '12 14:11

Nilesh Thakkar


People also ask

Why * is used in regex?

* - means "0 or more instances of the preceding regex token"

What does 2 mean in regex?

\*' matches the star character, and {2,}` means at least two times. Your regex will match any two or more consecutive star characters.

Does empty regex match everything?

An empty regular expression matches everything.


3 Answers

I suspect it has something to do with the fact that using the g modifier makes the engine remember its state.

Try removing the g modifier, you do not need it anyway. The engine will always try to find a match in the whole string. Hence, the g modifier does not really do anything in combination with the test function, but is rather used for match. You also don't need the i modifier by the way, since \w includes both lower-case and upper-case letters.

And one last thing. Regular expressions provide the convenient \W which is the opposite \w and thus equivalent to [^\w].

like image 93
Martin Ender Avatar answered Oct 13 '22 09:10

Martin Ender


OPTION 1

Use the constructor rather than the literal notation:

var reg = new RegExp('[^\w]','gi');

More about differences between the two here: https://developer.mozilla.org/en-US/docs/Core_JavaScript_1.5_Guide/Regular_Expressions?redirect=no

OPTION 2

Mark the end of the string with a $ sign:

var reg = /[^\w$]/gi;

OPTION 3

If your regular expression uses the "g" flag, you can use the exec method multiple times to find successive matches in the same string. When you do so, the search starts at the substring of str specified by the regular expression's lastIndex property (test will also advance the lastIndex property).

Source: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp/exec

Therefore, in your case the test will work only 1st, 3rd, 5th, ... time.

Only in Firefox 3 there is a flag y , when specified exec always starts from 0 not lastIndex but this is probably not useful in your case.

You can remove the g flag.

like image 42
sainiuc Avatar answered Oct 13 '22 09:10

sainiuc


It seems that the RegExp object is not as stateless as we think. A quick fix is to move var reg = /[^\w]/gi; inside the checkNonWordChars() method

Best Regards,

like image 1
Esteban Aliverti Avatar answered Oct 13 '22 07:10

Esteban Aliverti