Useful regular expressions


/ Published in: Regular Expression
Save to your folder(s)



Copy this code and paste it in your HTML
  1. //replace all but links
  2. /<(?!\/?a(?=>|\s.*>))\/?.*?>/g
  3.  
  4. //validate url
  5. /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \?=.-]*)*\/?$/
  6.  
  7. //Validate US phone number
  8. /^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/
  9.  
  10. //Test if a password is strong
  11. (?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$
  12.  
  13. //Validate US zip code
  14. ^[0-9]{5}(-[0-9]{4})?$
  15.  
  16. //Validate Canadian postal code
  17. ^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[ ]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$
  18.  
  19. //Grab unclosed img tags
  20. <img([^>]+)(\s*[^\/])>
  21.  
  22. //Find all CSS attributes
  23. \s(?[a-zA-Z-]+)\s[:]{1}\s*(?[a-zA-Z0-9\s.#]+)[;]{1}
  24.  
  25. //Get code within <?php and ?>
  26. <\?[php]*([^\?>]*)\?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.