Regular Expression Testing Tool
Details
This is a JavaScript powered tool that I've created to help myself test out regular expressions. This is only for JavaScript regular expressions since that's what's powering it. Though, most of works here should work in other languages as well. If you've messed with regular expressions the interface should be pretty straight forward. The text field is for your regular expression. Above it are three check boxes that you can set. Global means that the expression will be checked across the whole text rather than just the first occurrence. The second check box will make the expression case insensitive so that 'Good' is seen the same as 'good.' The third check box is for multi-line. This is helpful when using the ^ and $ syntax in a regular expression. The background color on this field will change between two colors. When it's green it means the regular expression is well-formed. When it turns yellow it means it has a typo. When it's yellow the button labeled "Check" above will become active and clicking it will present you with an error about the expression.
The text area below the expression area labeled Test Text is just that, a place to enter some text that you want to test your expression against. Then, below this area is a section that will take the test text and style it to show you what parts were matched. When there is no matches the background color for the test text will turn red. When the background is yellow it means there's a syntax error in the regular expression and any shown matches should not be considered valid. Matched text in the styled output will be colored blue to help tell it apart. There's no button the engage the check, it will update as you type or make changes.
Examples
Here are some examples I have to show you some expressions to try out and use.
Novice
[\w]{4,}
- Finds 4 characters or more in a row
- Matches
Four,doggy,ice456 - Non-matches
Cat,dog
[^\w]{4,}
- Finds 4 non-characters or more in a row
- The ^ just inside the square bracket causes it to negate the search criteria
- Matches
One??$#two,!@#$%^ - Non-matches
http://www,0n$^%you@*
^[\d\(\)\-]*$
- Detects numerical data that has parentheses and dashes
- This one will detect phone numbers
- Using the ^ at the beginning here stands for it being the start of the string and the $ at the end designates the end of the string
- Matches
(555)555-1234,555-555-1234,555(555)1234 - Non-matches
555-abc-1234
Intermediate
[b-df-hj-np-tv-z]{5,}
- Checks for nonsense words when 5 consecutive consonants exist
- Text matching this can indicate spam words
- Matches
werthgfui,peoplethlry - Non-matches
happiness
<(\/)?[a-zA-Z]+( ([^>]*))*>
- Finds HTML tags, but not the text inside of them.
- This does have a slight bug where if the > symbol is used anywhere inside the tag, e.g., alt="3 > 1 is true" the expression will not catch it correctly. This shouldn't be an issue though since it should be translated into an entity.
- Matches
<tag>,<tag id="attribute">,</tagger>,<img src="text" /> - Non-matches
<123>,<>,3<=1
^[\w\.\+_\-]+[\w]+@(([\w\-]+)\.)[a-zA-Z]{2,6}$
- This checks for a valid email address format
- The part before the @ can contain letters, numbers, underscores, periods, pluses, or dashes
- After the @ the text can only contain letters, numbers, and dashes
- The final part of the email address can only contain letters and must be between 2 and 6 characters long
- Matches
abc1223@empty.com,b@b.mom,t5+se-rt@com-com.nety - Non-matches
qw!erty@cc.nnn,ffff@com+ty.ney,w-wrt-ro.com
Advanced
.*(?=.*[A-Z]{2,}.*)(?=.*[a-z]{2,}.*).*
- Checks for abCD or ABcd type strings
- Useful for detecting spam words
- Matches
sorrYForit,IPhone - Non-matches
HaHa,OK,McHenry
^[a-zA-Z]((?=(.*[a-z].*){2,})(?=(.*[A-Z].*){2,})(?=(.*\d.*){2,})(?=(.*[!&\?#\<\>'].*){2,})(?!.*[$^\*\(\)\+\=\"\.\\\/\|\{\}\[\] \`\~\@])).{7,13}$
- This is something I put together for detecting a strong password
- It must start with a letter
- Must contain at least 2 lowercase letters, 2 uppercase
- Must contain at least 2 digits
- Must contain at least 2 special characters
- Special characters consist of !, &, ?, #, <, > (these of course should be fitted to the needs of the system)
- Password can between 8 and 14 characters
- Matches
aA1!bB2>,rtG?LO#123 - Non-matches
123abc!@#ASD,123ABCabc&
(http(s)?\:\/\/)?[\w\.-]+\.[\w]{2,4}(:[\d]{1,5})?(\/([\w\.\/\+\#\~\?\&\=\%-])*)*((\W)+|$)
- Checks for a valid URL text; non-strict
- The http:// part of the URL is optional
- Happens to match most file names as well, which can be remedied by making it strict by not making the http:// portion optional
- Matches
http://www.angelwatt.com,google.com,some.com/page.ht?q=wer,file.txt - Non-matches
http://angel@watt.com,domain.s/hha,www.fortune/page
References / Links
- A nice regular expression cheat sheet for quick reference
- Wikipedia's write up on regular expressions
- Regular-Expressions.Info a decent site, but a little on the technical side when it comes to reading it