Posted By

vagrantradio on 03/31/10


Tagged

regex


Versions (?)

Who likes this?

12 people have marked this snippet as a favorite

oleg
tiffle
NARKOZ
benrudolph
Tyster
lori
kurtmisc
riacharda
banjomamo
uberdragon
LeeRJohnson
ringo380


Useful regular expressions


 / Published in: Regular Expression
 

  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  

You need to login to post a comment.