Advertisement
Zekrommaster110

[JS] expanded string split function

Sep 19th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     split("hey ho what's up", ["-", "'", "."])
  3.     -> returns ["hey ho what", "s up"]
  4.    
  5.     split("hey ho what's up", ["-", "'", "."], true)
  6.     -> returns ["hey ho what", "'s up"]
  7. */
  8. function split(inp, identifiers, withAppend) {
  9.     if (typeof(withAppend) === 'undefined') withAppend = false;
  10.     for (var i in identifiers) {
  11.         var index = inp.indexOf(identifiers[i]);
  12.         if (index > -1)
  13.             return [inp.substr(0, index), inp.substr(withAppend ? index : index + 1, inp.length)];
  14.     }
  15.     return [inp];
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement