Javascript Regular Expression { PART 1}

Javascript Regular Expression { PART 1}

We have all been hearing regular expression or Regex , but what does it really mean, how do you write regular expression, what language can you use to write regular expression or regex, how do you understand regex, there are tons and tons of questions. But you know what say no more 😜😜 as we will be talking about Regex in this blog post and the next blog post to come. Sit tight as this is gonna be a long and fun journey, trust me πŸ™πŸ’ͺ

What is Regex

A regular expression is a sequence of characters that define a search pattern. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation. It is a technique developed in theoretical computer science and formal language theory.

Note: Languages like Python, Javascript, Java, C language and so on have regular expression, but in this article we will be talking about regex in Javascript. Are you excited ?

I will be concise as much as possible and I wont bore you with long talks. Lets get started :)

Creating a Regular Expression

There are two ways to create a regular expression in Javascript. It can be either created with RegExp constructor, or by using forward slashes ( / ) to enclose the pattern.

Regular Expression Constructor:
Syntax: new RegExp(pattern[, flags])

For example:

var regexConst = new RegExp('abc');

Regular Expression Literal:

Syntax: /pattern/flags

For example:

var regexLiteral = /abc/;
  • Here the flags are optional, I will explain these later in this article.

There might also be cases where you want to create regular expressions dynamically, in which case regex literal won’t work, so you have to use a regular expression constructor.

No matter which method you choose, the result is going to be a regex object. Both regex objects will have same methods and properties attached to them.

Since forward slashes are used to enclose patterns in the above example, you have to escape the forward slash ( / ) with a backslash ( \ ) if you want to use it as a part of the regex.

Regular Expressions Methods

There are mainly two methods for testing regular expressions. RegExp.prototype.test()

This method is used to test whether a match has been found or not. It accepts a string which we have to test against regular expression and returns true or false depending upon if the match is found or not.

For example:

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

// returns true
RegExp.prototype.exec()

This method returns an array containing all the matched groups. It accepts a string that we have to test against a regular expression.

For example:

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.

We are going to use the test() method in this article.

Simple Regex Patterns

It is the most basic pattern, which simply matches the literal text with the test string.

For example:

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

Thanks for reading, in the next article we will be talking about Special Characters in the next article, hold tight πŸš€πŸš€πŸš€