If you want to do it in one regular expression, without listing each case, you can use a lookahead assertion: /^(?=.*8.*)[0-9]{4}$/ The assertion (?=.*8.*) checks that the following matches the expression contained (.*8.*) which fails if there is not an 8. 2007/2/9, Peter Lauri <lists@xxxxxxxxxxx>:
Best group member, I want to match a four digit number. I allow user to enter with * syntax. So 8* would match anything that starts with 8 and is 4 digit long so: /^8[0-9]{3}$/ That was easy. Ok then my other case was: *8, so anything that ends with 8 /^[0-9]{3}8$/ Ok, now the tricky one comes: *8*, so match it incase 8 is anywhere in the number. Can be beginning, end or in the middle. The problem that I face I cannot find out a good way of doing this correctly. So I ended up with an expression like this: /^(8[0-9]{3}|[0-9]8[0-9]{2}|[0-9]{2}8[0-9]|[0-9]{3}8)$/ This takes care of it and everything, BUT it is so ugly. What I actually need to construct is: A regular expression that checks if 8 is a part of the number, and then that it is four digit long. The pipe "|" is an OR operator, but are there not any "AND" operator in Regular Expressions? I have been trying to figure this out for a while now. Of course I am using the above syntax right now, but would like to strip it down. Maybe not for the performance, but for the beauty of it :-) If you have any comments and suggestions about this I would be happy. Best regards, Peter Lauri <http://www.dwsasia.com/> www.dwsasia.com - company web site <http://www.lauri.se/> www.lauri.se - personal web site <http://www.carbonfree.org.uk/> www.carbonfree.org.uk - become Carbon Free