I use this a lot and I always have to look it up. I decided to blog it so that I don’t have to search every time I need to use Regular Expression Grouping to grab information from a string.

Let’s say that I have a string that I need to pull some data out of for another function. Regular Expressions are a powerful and reliable way to do this. When you combine Groups with Regular Expressions, they become even more handy.

Let’s say that I have a string that contains a domain name, a slash, and then a username. I only want the username.

You can easily pull the username out of the string by using a script just like the following:

1
2
3
4
5
var authString = "RadRoadTrips/johnandersen";
var tokens = /^.*\/(.*)$/(authString);
if(tokens && tokens.length > 1){
 alert("Username: " + tokens[1]);
}