Regular Expressions You Should Know

 

Matching a User Name

Pattern

/^[a-z0-9_-]{3,16}$/

Description:

Find the beginning of the string (^), followed by any lowercase letter (a-z), number (0-9), an underscore, or a hyphen. Next, {3,16} specifies that are at least 3 of those characters, but no more than 16. Last, get the end of the string ($).

String that matches:

s0me-usEr_n8me (hyphens and underscores allowed)

String that doesn’t match:

an-Extr3mely_L0ngusername (longer than 16 characters)

 

Matching an Email

Pattern:

/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/

Description:

Find the beginning of the string (^). Inside the first set of paranthesis, we match one or more lowercase letters, numbers, underscores, dots, or hyphens. (An escaped dot is shown here because a non-escaped dot means any character.) After the paranthesis is an @ symbol. Inside the second Next is the domain name which must be: one or more lowercase letters, numbers, underscores, dots, or hyphens. Then another (escaped) dot, with the extension being two to six letters or dots. I have 2 to 6 because of the country specific TLD’s (.ny.us or .co.uk). Finally, we want the end of the string ($).

String that matches:

john@doe.com

String that doesn’t match:

john@doe.something (TLD is too long)

 

Matching a URL

Pattern: 

/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/

String that matches:

http://chromaloop.com/about

String that doesn’t match:

http://google.com/some/file!.html (contains an exclamation point)

 

Matching an HTML Tag

Pattern:

/^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/ 

String that matches:

<a href=”http://chromaloop.com/”>chromaloop.com</a>

String that doesn’t match:

<img src=”img.jpg” alt=”My image>” /> (attributes can’t contain greater than signs)

 

Conclusion

Examples taken from a longer tutorial first posted on http://net.tutsplus.com/tutorials/other/8-regular-expressions-you-should...
There is some great discussion among some users who have stronger oponions about when and where it's appropriate to use regular expressions for production. For most users though, these are good examples to get you pointed in the right direction, and possibly useful for smaller code projects. Happy coding!

Speak your mind