Posted By

cuimingda on 10/15/09


Tagged


Versions (?)

pick a certain substring from a string by regular expression


 / Published in: JavaScript
 

URL: http://oragg.com/2009/10/snipplr-for-wordpress.html

  1. function pick(str, re) {
  2. var result = str.match(re);
  3. return (result && result.length === 2) ? result[1] : result;
  4. }
  5.  
  6. var str = "tel: 13512345678";
  7. var tel_a = pick(str, /tel:\s?(\d+)/);
  8. var tel_b = pick(str, /tel:(\d+)/);
  9.  
  10. // return 13512345678
  11. alert(tel_a);
  12.  
  13. // return null
  14. alert(tel_b);

Report this snippet  

You need to login to post a comment.