pick a certain substring from a string by regular expression


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your 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);

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.