IDIOT DEVELOPER

An Introduction to Regular Expression in JavaScript
An Introduction to Regular Expression in JavaScript

Regular Expression is one of the most important part of the JavaScript. They are especially used during string validation. So if you are new to Regular Expression and does not know anything about it, then don’t worry, this tutorial is for you.

 

Regular expressions are patterns used to match character combinations in strings.

 

Regular Expression are a piece of code that to detect pattern in a string. For example in a registration form, user enters the email address. We need to validate the email address , we can use regular expression to check whether the email address  is valid or invalid.

 

test@example.com is valid.

contact.mysite.com is invalid. It doesn’t contain the @ symbol.

 

Basics

Learning the basics of anything is key to success, like wise if you understood the basics of regular expression then you can master it easily.

 

Specify a regular expression like this –  /pattern/

 


AXBXXCXXXD.replace(/x+/g, "q");

 

1. ^ – Matches the begining of a string.

2. $ – Matches the end of a string.

3. .(dot) – Matches any character, except for line breaks.

4. \ – Escape , what would otherwise be a special character.

5. * – Matches zero or more occurrence of a character.

6. + – Matches one or more occurrence of a character.

7. ? – Matches zero or one occurrence of a character.

8. \d – Matches any single digit.

9. \w – Matches any word character (alphanumeric & underscore).

10. \W – Matches any non-word character.

11. \s – Whitespace character.

12. \S – Non-whitespace character.

13. {n} – Matches exactly n occurrences of the preceding expression.

14. {n,} – Matches at least n occurrences of the preceding expression.

15. [abc] – A character set. This pattern matches any of the character within the brackets.

16. [^abc] – A negated character set. This pattern do exactly opposite of the previous one, it matches every thing except the character within the brackets.

17. [a-z] – A range. This pattern matches any of the character within range.

 

Modifiers or Flags

Regular expressions have optional flags or modifiers that allow for global and case insensitive searching. These flags can be used separately or together in any order, and are included as part of the regular expression.

 

1. /pattern/g – Do global Search, find all matches.

2. /pattern/i – Do case-sensitive matching.

3. /pattern/m -Do multi-line matching.

 

Now we are going to discuss some of the functions or methods related to the regular expression.

 

Test()

This function test a pattern match with a given string. It requires a parameter – that is the string on which the operation needs to be done. It returns Boolean value. If match is found it returns true, otherwise false.

 

Example: We will check that the given string contains @ or not.

 


var re = /@+/;
re.test("nik");   //False
re.test("nik@tom");   //True

 

In the example above, we first declare the regular expression which allows only @ character. After that we add + sign, which means it will find one or more occurrence of the @ character in the string. Then we test the regular expression with two words, first nik which returns false as it doesn’t contain the @ character. The second the word is nik@tom which returns true as it contains the @ character.

 

Split()

This function takes the regular expression as an input parameter, and returns an array of all parts of the string that are in between the regular expression.

 

Example: We will split the email address into the username and the domain name.

 


var re = /@/;
var str = "nikhil@gmail.com";
var data = str.split(re)
//data = ["nikhil", "gmail.com"]
data[0]   //nikhil
data[1]   //gmail.com

 

In the example above we declare the regular expression which only allow @ character. After that we declare the string and then we apply split() function on it. It returns an array.

 

Match()

This expression takes regular expression as an input parameter and returns an array of parts of string that match the regular expression. We can its some what opposite of the split(), as split returns string part that does not matches the regular expression , and match returns part that matches the regular expression.

 

Example: We will find the character X in the string.

 


var re = /X+/g;
var str = "aXbXXcXXXd";
var data = str.match(re)
//data = ["X", "XX", "XXX"]

 

In the example above we first declare the regular expression, then we declare the string. We do match operation on that string.

 

Replace()

This function takes two parameter, first the regular expression and second the word or character which will be replacing the other character or word. This function replaces all the places that matches the regular expression with a replacement string.

 

Example: We will replace a word everyone in the string with world.

 


var re = /everyone+/
var str = "Hello, everyone";
var data = str.replace(re, "world");
//data = "Hello, world"

 

In the example above we first declare the regular expression, then we declare the string. We do replace operation on that string, it replaces the everyone with the world.

 

Leave a Reply

Your email address will not be published. Required fields are marked *