Take the 2-minute tour ×

I want to make a regular expression for my university registration number. I am 75% successful to make it. I'm new and i don't know how to make it. This is what i am doing.

<!DOCTYPE html>

str = "l1s10bscs"; //successfully tested
                   //but i want to append any 4 digits at the end of l1s10bscs

re = /[a-zA-Z]\d{1}[s|S|f|F]\d{2}[bscs]/g;

result = re.test(str);




i tried this but it doesn't work.

re = /[a-zA-Z]\d{1}[s|S|f|F]\d{2}[bscs][0-9]{4}/g;  // this doesn't work 
share|improve this question
It's not clear what you're trying to match. Can you give us a set of strings that should match your regex? –  AmericanUmlaut Dec 5 '12 at 20:29
1: l1s10bscs2034 2: L1F10BSCS1111 it should work well with these sort of strings. –  dEvILs adVocAte Dec 5 '12 at 20:30
Why are you using [0-9]{4} instead of \d{4}? –  Shmiddty Dec 5 '12 at 20:32
So "bscs" should always be in the string in exactly that order? –  AmericanUmlaut Dec 5 '12 at 20:33
You might want to put that down as an answer, @AmericanUmlaut. The previous one had a match even though it only matched the first single character in the "bscs" part of the string. –  Maarten Bodewes - owlstead Dec 5 '12 at 20:34

3 Answers 3

up vote 3 down vote accepted

According to your description, this should fit.

I changed the following:

  • [bscs] means "b or s or c or s" (so "b or s or c", the extra s is meaningless). You wanted just the literal four-character string "bscs"
  • \d{1} is the same as \d - this isn't an error, but there's no reason to explicity define a character as occurring only once.
  • You have a \g flag but no \i, so you'll match the string you're looking for multiple times inside of a larger string, but your search isn't case insensitive.
  • [s|S|f|F] means "s or | or S or |..." You meant "s or S or f...", which is written [sSfF].
  • Since \i is used to make a search case insensitive, I simplified [sSfF] to [sf]
share|improve this answer
Do you need to match the bscs? Wouldn't omitting the () work just as well? –  Guvante Dec 5 '12 at 20:37
@Guvante yes, that would still work. –  Maarten Bodewes - owlstead Dec 5 '12 at 20:38
[s|f] means "s" OR "|" OR "f". Shouldn't that be [sf] or (s|f)? –  Rocket Hazmat Dec 5 '12 at 20:39
@RocketHazmat Yep. Fixed. Also, I just realized that there's no reason to explicitly define {1} for the first digit. –  AmericanUmlaut Dec 5 '12 at 20:41
@dEvILsadVocAte I'm not an expert on regex best practices, but I'd give the following feedback on the above: As Rocket Hazmat said, wrapping single characters in [] is meaningless. You're excluding lots of legal characters (eg , . - _) from the local part of the address (see en.wikipedia.org/wiki/Email_address). Technically the local part of an e-mail address - but not the domain - is case sensitive, though I think it's very common to treat it as insensitive. You won't recognize googlemail.com, even though that's equivalent to gmail.com. –  AmericanUmlaut Dec 6 '12 at 9:34

The [bscs] needs to not be in square brackets: square brackets means b or s or c or s. It works without the number because it matches "l1s10b", but the next character is not a digit so looking for 4 digits fails. Try this:

re = /[a-zA-Z]\d{1}[s|S|f|F]\d{2}bscs[0-9]{4}/g;
share|improve this answer
[s|S|f|F] doesn't do what you think it does. –  Rocket Hazmat Dec 5 '12 at 20:41
Yep, you are right, the | characters do not belong in there. –  EvilBob22 Dec 5 '12 at 20:47

You seem to be confusing [] and ().

() creates a group, whereas [] means "match any letter from inside".

[s|S|f|F] means 's' OR '|' OR 'S' OR 'f' OR 'F'. That should be (s|S|f|F) or [sSfF].

NOTE: You can use the i modifier to make it case-insensitive.

re = /[a-z]\d[sf]\d{2}bscs\d{4}/gi;
share|improve this answer
Good point on the | in the []. It may or may not be acceptable to make the entire match case-insensitive though. –  EvilBob22 Dec 5 '12 at 20:47
@RocketHazmat i have another question. What if i want to restrict a character like hyphen '-' in my email address RE, what should i do then? –  dEvILs adVocAte Dec 5 '12 at 21:15
i mean an email add can be of type alphanumeric but it cannot have some other characters in email like hyphen '-', semicolon ';' and coma ','. How to restrict them in RE? –  dEvILs adVocAte Dec 5 '12 at 21:18
@dEvILsadVocAte: Inside [] the ^ character means "not". For example [^;] is "any character that isn't ;". –  Rocket Hazmat Dec 5 '12 at 21:32

Your Answer


