Skip to content

Instantly share code, notes, and snippets.

@westc
Last active August 29, 2015 14:05
Show Gist options
  • Save westc/18dff2998b00abecebd6 to your computer and use it in GitHub Desktop.
Save westc/18dff2998b00abecebd6 to your computer and use it in GitHub Desktop.
Easily insert a string within another string.
String.prototype.insert = function(strToInsert, num_rgx_str, opt_after) {
return ({}).toString.call(num_rgx_str) == '[object Number]'
? this.slice(0, num_rgx_str + strToInsert + this.slice(num_rgx_str)
: this.replace(num_rgx_str, function(match) {
return opt_after ? match + strToInsert : (strToInsert + match);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment