Posted By

troynt on 04/15/09


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

bigheadlyf


Get String between a Prefix String and a Suffix String


 / Published in: JavaScript
 

URL: http://userscripts.org/scripts/review/35396

  1. /**
  2.  * Usage var you = 'hello you guys'.between('hello ',' guys');
  3.  * you = 'you';
  4.  */
  5. String.prototype.between = function(prefix, suffix) {
  6. s = this;
  7. var i = s.indexOf(prefix);
  8. if (i >= 0) {
  9. s = s.substring(i + prefix.length);
  10. }
  11. else {
  12. return '';
  13. }
  14. if (suffix) {
  15. i = s.indexOf(suffix);
  16. if (i >= 0) {
  17. s = s.substring(0, i);
  18. }
  19. else {
  20. return '';
  21. }
  22. }
  23. return s;
  24. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: bigheadlyf on April 15, 2009

Simple but Useful. Thanks

You need to login to post a comment.