Regex with Javascript Part 2

In the previous post, we have started Regex with JavaScript and discussed some important points of Regex in JavaScript.

In today’s post, we are going to continue with the same topic.

Match Single Character with multiple possibilities

In the previous, we have discussed match pattern where we had dealt with a string pattern (/string/) and wildcard both (/./) and both have its own significance.

Where one finds the exact match and one works with everything.
We can also enhance this search using Character classes.
Character classes allows us to define a group of characters after placing them in square bracket ([ ]).

Example:



/**
 *  range number
 */


 let emailString = "krdheeraj51@gmail.com";
 let regexString = /[a-z@0-9]/g;
 console.log(emailString.match(regexString));

OutPut:

Match letters of Alphabet

In the case of Character match pattern, we can also define a range.
Suppose we have taken all characters from are then we can define our character classes like [a-e];

Example:



/**
* Character classes
*/

let catStr = "cat";
let batStr = "bat";
let ratStr = "rat";
let bgRegex = /[a-e]at/;

console.log(catStr.match(bgRegex));
console.log(batStr.match(bgRegex));
console.log(ratStr.match(bgRegex));

OutPut:

Match Numbers and letters of Alphabet

( – ) is not limited to matching range of character as we have seen previously. But it will also work to match a range of numbers.
For matching a number in between 10 to 15 we can write down like [ 10-15 ].

Example:



/**
*  range number
*/

let emailString = "krdheeraj51@gmail.com";
let regexString = /[a-z@0-9]/g;
console.log(emailString.match(regexString));

OutPut:

Match Single Characters not specified

Till now we have created a set of characters that we want to match.
We can also create a set of characters that we don’t want to match and these types of character sets are known as negated character set.

For using the negated character set we have to use caret character ( ^ ).

Example :

previous post

/**
* Ignore 0 to 9 and all vowel character
*
**/previous post

let quoteSample ="4th Umpire";
//create a regex for Ignore 0 to 9 and all vowel character
let myRegex = /[^0-9^aeiou]/gi;
console.log(quoteSample.match(myRegex));

OutPut:

Match Character that Occurs one or more time

Some times we need to match a group of Character that appears one or more than once.

We can check this case using + character. 

Remember::  In this case Character should be repeated consequently.

It means Character is being repeated one after another.

previous post suppose our String is “abc” and we are using our regex /a+/g then it will return [“a”].

But if our String is like “aabc” then it will return [“a”,”a”].

Example :



/**
* Handle case of matching zero or more Character
*/


let myString = "fuuuuuuuuuuun!!";
let realString = "It is funny time."
let moreString = "Not Interested.";

let myRegex = /fu*/i;

console.log(myString.match(myRegex));
console.log(realString.match(myRegex));
console.log(moreString.match(myRegex));

OutPut:

Match Characters that Occur Zero or More Times

The previous scenario handled the situation of occurring a Character one or more times.
At this time we will handle the same case of occurring a Character that may be occurred zero or more times.
Fo that that purpose we will use * Character.

Example:



/**
* Handle case of matching zero or more Character
*/

let myString = "fuuuuuuuuuuun!!";
let realString = "It is funny time."
let moreString = "Not Interested.";

let myRegex = /fu*/i;

console.log(myString.match(myRegex));
console.log(realString.match(myRegex));
console.log(moreString.match(myRegex));

OutPut:

 

More Post on Regex

Regex with Javascript

 

Leave a Reply

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