Understanding String.match

"string".match() is often a misunderstood or neglected part of JavaScript. I want to bring some focus on this neat tool and talk about some of its usage. The most common use of this method is to check if a string follows a particular pattern, or includes some bit of text:

1 var myDomain = document.domain; // (on this site, this is www.natehunzaker.com)
3 if ( myDomain.match("natehunzaker.com") ) {
4   console.log("We must be at the correct domain");
5 }

Dead simple, but this doesn't do this method justice. In addition string comparision, .match() also can utilize regular expressions. When used without the global option (/reg/g) .match() can extract additional meaning out of a statement simply by wrapping a rule within parenthesis. These are known as capturing parenthesis:

1 var myString = "Hello, world! My name is Nate Hunzaker!";
2 var name = myString.match(/My name is (.*) (.*)!/);
4 console.log( name[1] ) //=> Nate
5 console.log( name[2] ) //=> Hunzaker
6 console.log( name )    //=> ['My Name is Nate Hunzaker!', 'Nate', 'Hunzaker']

Pretty neat. This is particularly useful for extracting patterns of data out of bodies of text. For instance, a while back I needed to map some attributes an object based upon a key/value system within a large body of text:

 1 /*
 2 We'll assume the following content has been saved to the "story" variable:
 4 TITLE: The War of the Worlds
 5 AUTHOR: H.G. Wells
 6 BODY: No one would have believed in the last years of the nineteenth century that this world was being watched keenly and closely by intelligences...
 8 */
10 var pattern = "(.*)\\:(.*)\n";
11 var lines = story.match( new RegExp(pattern, "g") );
12 var data = {};
14 lines.forEach(function(line) {
15  var match = line.match( new RegExp(pattern) );
17  if (match) {
18      var key = match[1].toLowerCase();
19      var val = match[2].trim();
20      data[key] = val;
21  }
22 });
24 console.log(data.title);  //=> "The War of the Worlds"
25 console.log(data.author); //=> "H.G. Wells
26 console.log(data.body);   //=> "No one would have believed..."

Unfortunately JavaScript lacks Ruby's awesome String.scan() method, but we can make due by first finding all of the matches globally and then parsing each line individually. Generally speaking I have found this to read better and write much cleaner than cruder methods such as .split().

View the gist here