PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/chapter14/example_regex.txt

https://github.com/austinsprawls/js-jquery-missing-manual
Plain Text | 24 lines | 20 code | 4 blank | 0 comment | 0 complexity | dd7caeede55791a60f35dc675cb1d4e1 MD5 | raw file
  1. US Zip Code REGEX
  2. var ZipCode = /\d{5}(-\d{4})?/;
  3. if you want to make sure the string ONLY contains a Zip Code use the ^ and $ to match the beginning and ending of the string like this:
  4. var ZipCode = /^\d{5}(-\d{4})?$/;
  5. US Phone Number REGEX
  6. var phoneNum = /\(?(\d{3})\)?[ -.](\d{3})[ -.](\d{4})/;
  7. if you want to make sure the string ONLY contains a phone number use the ^ and $ to match the beginning and ending of the string like this:
  8. var ZipCode = /^\(?(\d{3})\)?[ -.](\d{3})[ -.](\d{4})$/;
  9. Email Address REGEX
  10. var email = /[-\w.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}/;
  11. if you want to make sure the string ONLY contains an email address use the ^ and $ to match the beginning and ending of the string like this:
  12. var email = /^[-\w.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}$/;
  13. Date REGEX
  14. var date = /([01]?\d)[-\/ .]([0123]?\d)[-\/ .](\d{4})/;
  15. if you want to make sure the string ONLY contains a date use the ^ and $ to match the beginning and ending of the string like this:
  16. var date = /^([01]?\d)[-\/ .]([0123]?\d)[-\/ .](\d{4})$/;
  17. URL REGEX
  18. var URL = /((\bhttps?:\/\/)|(\bwww\.))\S*/;
  19. if you want to make sure the string ONLY contains a URL use the ^ and $ to match the beginning and ending of the string and remove the \b characters like this:
  20. var URL = /^((https?:\/\/)|(www\.))\S*$/;