Yesterday I added a post about
String.prototype.before(...). As is explained within the post, this function can be used to easily extract a substring before a given target. Of course, if you can get what comes before a target you should be able to get what comes after a target too, right? Here is a function that makes that possible:
As indicated in the comments, this function can take a target (string or regular expression) to key off of to extract the desired substring. It also takes an optional second argument used for indicating the occurrence of the target to key off of. For instance, if you wanted to get the string that comes after the second comma in
"Uno, dos, tres, cuatro, y cinco" you could use the following code:
var str = "Uno, dos, tres, cuatro, y cinco"; var afterFirstTwo = str.after(',', 2);
Here are some other examples that I used to test this function:
As usual, feel free to use this function in your own projects. Have fun!