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.
- Basic JavaScript
- Regular/Regex Expression : To Learn Regex if you don't know do click here.
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"]
-
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 shdd@gmail.com.
- 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