Yesterday I finished up with the ES6 and most of regular expression portion in freecodecamp. I was surprised at how long the course regarding to just one function. Just goes to show how complex regular expressions are. There were a lot of different scenarios of using it.

Regular expressions allow you to check strings of characters and describe patterns in the data. There are regular expressions and regular expression literals. For most of the problems in freecodecamp, I used the regular expression literals. So I would assume that is the more common form of using it. As such I will be covering using it in the literal way.

Regular expression literals this is the syntax

var regexLiteral = /abc/;

Beyond the syntax, there are also methods of using regular expressions. The first method was RedExp.prototype.test(). This method tests the string with the variables you are looking for and returns back true or false.

For example:

var regex = /hello/;
var str = 'hello world';
var result = regex.test(str);
console.log(result);
// returns true

RedExp.prototype.exec() is the second method which is only different because it returns back an array.

var regex = /hello/;
var str = 'hello world';
var result = regex.exec(str);
console.log(result);
// returns [ 'hello', index: 0, input: 'hello world', groups: undefined ]
// 'hello' -> is the matched pattern.
// index: -> Is where the regular expression starts.
// input: -> Is the actual string passed.

Regular expressions have 5 optional flags which 2 of them are the most commonly used. The 2 most common flags are g and i. G- is a global flag which keeps searching for all matches instead of just stopping on first match. The I flag is used for making matches ignore capitalization.

There are a lot more complex nuances with regular expressions but those are the very basics that I took away from it.

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: