[thelist] RegEx problem again

Kasimir K evolt at kasimir-k.fi
Tue Aug 9 04:09:52 CDT 2005


Matt Warden scribeva in 2005-08-08 21:45:
> I believe he said the asterisk could be present anywhere in the string.

Ah, indeed he did - and that complicates things a bit :-) The question 
then is whether we allow multiple asterisks or just one. And if many, do 
we still require at least three letters?

As javascript doesn't support lookbehinds or conditionals, I'd say that 
this can't be accomplished with just one check. So I'd first check that 
letters, spaces and apostrophes are as they should, allowing asterisks 
if they are not followed by another asterisk, and after that check that 
asterisks and the length is correct.

So first check becomes (omitting here alphas other than A-z for simplicity):
^([A-Za-z]|\*(?!\*))([A-Za-z\*]|( (?![ ']))|('(?!'))|\*(?!\*)){2,}$

And then check for asterisks:

var hasAtLeastOneAsterisk = /^.*\*.*$/.test(nametext.value);
var hasMaxOneAsterisk = /^[^\*]*\*?[^\*]*$/.test(nametext.value);

If we have at least one asterisk, check that nametext.value is at least 
four characters long. If we allow many asterisks, then we should also 
count them: for n asterisks the string must be at least 3 + n characters 
long.

It would be interesting to see if a RegEx guru could squeeze all this 
into just one check... I suspect it would be quite a best of a RegEx, 
especially considering the limitations of JavaScript's implementation.

.k


More information about the thelist mailing list