Lets build a Email Extractor With the Knowledge of Regex in JavaScript.

Lets build a Email Extractor With the Knowledge of Regex in JavaScript.

SURYA L's photo
SURYA L
·Jun 30, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • Prerequisite:
  • Program build a Email Extractor in JavaScript.
  • Explanation:
  • To Extract Special email extension like @gmail.com

Hello everyone hope you are all doing well, My name is Surya L.

The aim of this blog is to build a Email Extractor With the Knowledge of Regex in JavaScript.

Prerequisite:

  • Basic JavaScript
  • Regular/Regex Expression : To Learn Regex if you don't know do click here.

Program build a Email Extractor in JavaScript.

const emailExtractor=(text)=>{
     let emailIds = text.match(
      /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi
    ); 
    return emailIds;
}
console.log(emailExtractor("bhsdbknhjdbhkjnldsjhbdfk@yahoo.jh ss@yahoo.com dajfguydcbxheydfsgv shdd@gmail.com"));

The Output for the above Code is :

["bhsdbknhjdbhkjnldsjhbdfk@yahoo.jh", "ss@yahoo.com", "shdd@gmail.com"]

Explanation:

  • We created a function known as emailExtractor which takes text as parameter(value).

  • Variable emailIds will match /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi

  • In which flag - i is used to ignore Uppercase and lowercase and match the value.

  • To search or extract a pattern more than once,we use the global search flag: g.
  • /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi Is divided into 3 parts.

  • [a-zA-Z0-9._-] matches Alpha Numeric Values Basically Alphabets and Numbers.

  • @[a-zA-Z0-9._-]The start of the text/value should start with @ symbol and matches Alpha Numeric Values Basically Alphabets and Numbers.

  • .[a-zA-Z0-9_-] The start of the text/value should start with . symbol and matches Alpha Numeric Values Basically Alphabets and Numbers.

  • And By adding all three values we get email values like .

  • Finally we return the value of emailIds variable with return keyword.

To Extract Special email extension like @gmail.com

const emailExtractor=(text)=>{
     let emailIds = text.match(
      /([a-zA-Z0-9._-]+@[gmail]+\.[a-zA-Z0-9_-]+)/gi
    ); 
    return emailIds;
}
console.log(emailExtractor("bhsdbknhjdbhkjnldsjhbdfk@yahoo.jh ss@yahoo.com dajfguydcbxheydfsgv shdd@gmail.com"));

The output of above Code is ["shdd@gmail.com"] Thanks for reading the blog. Do let me know what you think about it.

You can connect me with Showwcase Twitter Blog GitHub Contact Me

Did you find this article valuable?

Support SURYA L by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this