Get String between a Prefix String and a Suffix String


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



Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.