Node.js: Regular Expression to read email from string - javascript

How can I take an e-mail address from "XXX <email#email.com>" ? I don't want to get the "< >".
Thanks!

Here's one based on Tejs' answer. Simple to understand and I think a bit more elegant
// Split on < or >
var parts = "XXX <email#email.com>".split(/[<>]/);
var name = parts[0], email = parts[1];

Really simply (no need for regex!)
var components = emailAddress.split('<')
if(components.length > 1)
{
var emailAddress = components[1].replace('>', '');
}

function getEmailsFromString(input) {
var ret = [];
var email = /\"([^\"]+)\"\s+\<([^\>]+)\>/g;
var match;
while ( match = email.exec(input) ) {
ret.push({'name': match[1], 'email': match[2]});
}
return ret;
}
var str = '"Name one" <foo#domain.com>, ..., "And so on" <andsoon#gmx.net>';
var emails = getEmailsFromString(str);
credit: How to find out emails and names out of a string in javascript

^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*#[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$
Matches e-mail addresses, including some of the newer top-level-domain extensions, such as info, museum, name, etc. Also allows for emails tied directly to IP addresses.

This regex will work for your example.
/<([^>]+)/
It searches for anything after the '<' that is not a '>' and that is returned in your matches.

To just grab what's inside the angle brackets, you can use the following:
var pattern = /<(.*)>/;
pattern.exec("XXX <foo#bar.com>"); // this returns ["<foo#bar.com>", "foo#bar.com"]

Not positive if I'm understanding you correctly. If you want to get the email domain ie gmail.com or hotmail.com then you could just use
var x =string.indexOf("#"); var y =string.subString(x)
this will give you the string y as the email domain.

Related

Regular expression for extracting domain from an input

I'm trying to extract the domain.com from an input that can be in the following formats and structure:
1. x.x.domain.com
2. x.domain.com
Once I am getting user's email, for example:
user#x.x.domain.com
I am able to remove the first part of the email address:
user#
by the following regex:
/^.+#/
I want to be able by using the regex over the 2 formats to get the domain.com right away and not manipulate the input several times until getting the domain.
I thought maybe to count the number of dots from the input and then to do some logic, but it looks so complex for this small solution.
Thanks!
Without RegExp: split the e-mail address twice, slice from the last split and join the result. Plus two RegExp ideas. Take your pick.
const getDomain = address =>
address.split("#")[1].split(".").slice(-2).join(".");
const getDomainRE = address =>
address.match(/\.\w+/g).slice(-2).join("").slice(1);
const getDomainRE2 = address =>
address.match(/(?:(#|\.)\w+){2}$/)[0].slice(1);
console.log(getDomain("user#x.x.domain.com"));
console.log(getDomain("user#x.domain.com"));
console.log(getDomain("user#abc.x.y.z.domain.com"));
console.log(getDomainRE("user#x.x.domain.com"));
console.log(getDomainRE("user#x.domain.com"));
console.log(getDomainRE("user#abc.x.y.z.domain.com"));
console.log(getDomainRE2("user#x.x.domain.com"));
console.log(getDomainRE2("user#x.domain.com"));
console.log(getDomainRE2("user#abc.x.y.z.domain.com"));
Regex way:
let str1 = "x.domain.com";
let str2 = "x.sdfsdf.google.com";
let str3 = "www.subdomain.yahoo.com";
let reg = /[^.]+\.[^.]+$/gi;
console.log(str1.match(reg)); // domain.com
console.log(str2.match(reg)); // google.com
console.log(str3.match(reg)); //yahoo.com
Simple javascript:
function getDomain(str){
let arr = str.split(".");
if(arr.length < 2){
console.log("Invalid domain name");
return null;
}
else{
return `${arr[arr.length-2]}.${arr[arr.length-1]}`;
}
}
let str1 = "x.domain.com";
let str2 = "x.sdfsdf.google.com";
let str3 = "www.subdomain.yahoo.com";
console.log(getDomain(str1)); // domain.com
console.log(getDomain(str2)); // google.com
console.log(getDomain(str3)); //yahoo.com
This is regular expression for domain name
"[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$"
should also manage x.x.domain.co.uk
original pattern uses
"(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]"
Good luck
I know you didn't specifically ask for it, but you may want to consider country codes a top level domains as well (e.g. .au, .uk).
If required, you could achieve it with the following:
function getDomainFromEmail(domain) {
const domainExpression = /((\w+)?(\.\w+)(\.(ad|ae|af|ag|ai|al|am|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bl|bm|bn|bo|bq|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cw|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|er|es|et|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mf|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|ss|st|sv|sx|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|za|zm|zw))?)$/i;
const match = domainExpression.exec(domain);
return match ? match[1] : null;
}
console.log(getDomainFromEmail('email#example.com'));
console.log(getDomainFromEmail('email#sub.example.com'));
console.log(getDomainFromEmail('email#sub.sub.example.com'));
console.log(getDomainFromEmail('email#example.com.au'));
console.log(getDomainFromEmail('email#example.co.uk'));
console.log(getDomainFromEmail('email#sub.example.co.uk'));
console.log(getDomainFromEmail('email#sub.sub.example.co.uk'));
console.log(getDomainFromEmail('email#bit.ly'));
console.log(getDomainFromEmail('email#domain.other'));
console.log(getDomainFromEmail('email#nomatch'));
The long expression (ad|ae|...|zm|zw) is a list of country codes combined into a regular expression.
How about
(#).*
The first group of the output is # and the second group is domain.com

Extract email address from string

I have a string like this:
Francesco Renga <francesco_renga-001#gmail.com>
I need to extract only the email, i.e. francesco_renga-001#gmail.com.
How can I do this in nodejs/javascript in "elegant" way?
Using regex, if your string pattern is always Some text<email> or Some text<email>, Some text<email> <email> you can use this simple one <(.*?)>
Demo
Other solution
Use positive lookahead : [^<]+(?=>), here is a snippet and a demo
var text = "Francesco Renga <francesco_renga-001#gmail.com>, Marty McFly <mmcfly#gmail.com> Marty McFly <mmcfly#gmail.com> <mmcfly2#gmail.com>";
var re = /[^< ]+(?=>)/g;
text.match(re).forEach(function(email) {
console.log(email);
});
Explanation
[^<]+ match anything but a <between one and unlimited times
(?=>) followed by a >
Simple and does not require any group.
Here's a simple example showing how to use regex in JavaScript :
var string = "Francesco Renga <francesco_renga-001#gmail.com>"; // Your string containing
var regex = /<(.*)>/g; // The actual regex
var matches = regex.exec(string);
console.log(matches[1]);
Here's the decomposition of the regex /<(.*)>/ :
/ and / are mandatory to define a regex
< and > simply matches the two < and > in your string
() parenthesis "capture" what you're looking for. Here, they get the mail address inside.
.* : . means "any character", and * means "any number of times. Combined, it means "any character any number of times", and that is inside < and >, which correspond to the place where the mail is.
Here's a simple code showing how extract the unique list of emails address using JavaScript :
let emaillst = string .match(/([a-zA-Z0-9._+-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
if (emaillst === null) {
// no Email Address Found
} else {
const uniqueEmail = Array.from(new Set(emaillst));
const finaluniqueEmail = [];
for(let i=0; i<=uniqueEmail.length; i++){
let characterIs = String(uniqueEmail[i]).charAt(String(uniqueEmail[i]).length - 1)
if(characterIs==='.'){
finaluniqueEmail.push(String(uniqueEmail[i].slice(0, -1)))
}else{
finaluniqueEmail.push(uniqueEmail[i]);
}
}
emaillst = finaluniqueEmail.join('\n').toLowerCase();
console.log(matches[1]);
See the Live Demo of email address extractor online
Features
Get Unique Emails
Auto remove duplicate emails
convert upper case email address to lowercase

google apps script conditional if statement

I'm fairly new to GAS but learning fast. Thanks in advance for any help you can offer on this!!
I need to be able to send an email to two different email addresses based on the first characters of my last name field. So, if the first character of var last is A - L, send the email to this person, otherwise, if the first character is M - Z, send to this other person. How would I create a conditional if statement for this? My current script which currently sends all email to the same address is as follows:
function onFormSubmit(e) {
var timestamp = e.values[0];
var last = e.values[3];
var first = e.values[4];
var employment_status = e.values[5];
var unemployed_date = e.values[6];
var employment_type = e.values[7];
var employer_name = e.values[8];
var employer_address = e.values[9];
var employment_begin = e.values[10];
var comments = e.values[11];
var email = "xxxxx#gmail.com";
var subject = "Employment Update Received";
var message = "<HTML><BODY>"
+ "<P>Dear " + first + " " + last + ","
etc. etc.
+ "</HTML></BODY>";
MailApp.sendEmail(email, subject, "", {htmlBody: message});
}
#JonnyJS is on the right track. I used just regular expressions. Regex literals are wrapped in / / and options occur after the last /. I usually use RegexPal to test regular expressions for GAS. You can find more information about JS Regex on MDN. Most notably, the test function that I use below.
Here's the regex I used:
^ Starts of string
[A-L] Any letter A-L
.* Any character, 0 or more times
$ End of String
i Option for case insensitive regex
Here is a snippet for you to use.
var last = e.values[3].trim(); // Remove any leading spaces to be safe
var email = "Catch-Other-Names#mydomain.com";
var reAtoL = /^[A-L].*$/i;
var reMtoZ = /^[M-Z].*$/i;
if (reAtoL.test(last)) {
email = "A-to-L-Recipient#mydomain.com";
} else if (reMtoZ.test(last)) {
email = "M-to-Z-Recipient#mydomain.com";
}
// If both fail, then it's going to the Catch Other Names
Mail.sendEmail(email, subject, "", {htmlBody:message});
You havent been really clear but i hope i got what you meant.
I solve it by using two built in js stuff.
we cut the string to the first latter.
we matching the cutted part to regular expression.
Consider view that on JSFIDDLE
lets write the markup first:
<form>
<input name="last" type="text"/></form>
<br><br>
<div id="status">
</div>
now some JS:
var rel = new RegExp("[a-l A-L]");
var rez = new RegExp("[m-z M-Z]");
$("form input").on("change",function(e){
vall = $("form input").val().substr(0,1);
console.log(vall);
if (rel.exec(vall)) {
$("#status").text("We Got a winner! the last name starts with letter from a-l");
}
else if (rez.exec(vall)) {
$("#status").text("Hodey! the last name starts with letter from m-z");
}
else {
$("#status").text("Well, Nothing cool here.");
}
});
as you can see im using jquery to prettify things.
if you still in trouble please write in comments.

Extract all email addresses from bulk text using jquery

I'm having the this text below:
sdabhikagathara#rediffmail.com, "assdsdf" <dsfassdfhsdfarkal#gmail.com>, "rodnsdfald ferdfnson" <rfernsdfson#gmail.com>, "Affdmdol Gondfgale" <gyfanamosl#gmail.com>, "truform techno" <pidfpinfg#truformdftechnoproducts.com>, "NiTsdfeSh ThIdfsKaRe" <nthfsskare#ysahoo.in>, "akasdfsh kasdfstla" <akashkatsdfsa#yahsdfsfoo.in>, "Bisdsdfamal Prakaasdsh" <bimsdaalprakash#live.com>,; "milisdfsfnd ansdfasdfnsftwar" <dfdmilifsd.ensfdfcogndfdfatia#gmail.com>
Here emails are seprated by , or ;.
I want to extract all emails present above and store them in array. Is there any easy way using regex to get all emails directly?
Here's how you can approach this:
HTML
<p id="emails"></p>
JavaScript
var text = 'sdabhikagathara#rediffmail.com, "assdsdf" <dsfassdfhsdfarkal#gmail.com>, "rodnsdfald ferdfnson" <rfernsdfson#gmal.com>, "Affdmdol Gondfgale" <gyfanamosl#gmail.com>, "truform techno" <pidfpinfg#truformdftechnoproducts.com>, "NiTsdfeSh ThIdfsKaRe" <nthfsskare#ysahoo.in>, "akasdfsh kasdfstla" <akashkatsdfsa#yahsdfsfoo.in>, "Bisdsdfamal Prakaasdsh" <bimsdaalprakash#live.com>,; "milisdfsfnd ansdfasdfnsftwar" <dfdmilifsd.ensfdfcogndfdfatia#gmail.com> datum eternus hello+11#gmail.com';
function extractEmails (text)
{
return text.match(/([a-zA-Z0-9._+-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
}
$("#emails").text(extractEmails(text).join('\n'));
Result
sdabhikagathara#rediffmail.com,dsfassdfhsdfarkal#gmail.com,rfernsdfson#gmal.com,gyfanamosl#gmail.com,pidfpinfg#truformdftechnoproducts.com,nthfsskare#ysahoo.in,akashkatsdfsa#yahsdfsfoo.in,bimsdaalprakash#live.com,dfdmilifsd.ensfdfcogndfdfatia#gmail.com,hello+11#gmail.com
Source: Extract email from bulk text (with Regular Expressions, JavaScript & jQuery)
Demo 1 Here
Demo 2 Here using jQuery's each iterator function
You can use this regex:
var re = /(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/g;
You can extract the e-mails like this:
('sdabhikagathara#rediffmail.com, "assdsdf" <dsfassdfhsdfarkal#gmail.com>, "rodnsdfald ferdfnson" <rfernsdfson#gmail.com>, "Affdmdol Gondfgale" <gyfanamosl#gmail.com>, "truform techno" <pidfpinfg#truformdftechnoproducts.com>, "NiTsdfeSh ThIdfsKaRe" <nthfsskare#ysahoo.in>, "akasdfsh kasdfstla" <akashkatsdfsa#yahsdfsfoo.in>, "Bisdsdfamal Prakaasdsh" <bimsdaalprakash#live.com>,; "milisdfsfnd ansdfasdfnsftwar" <dfdmilifsd.ensfdfcogndfdfatia#gmail.com>').match(re);
//["sdabhikagathara#rediffmail.com", "dsfassdfhsdfarkal#gmail.com", "rfernsdfson#gmail.com", "gyfanamosl#gmail.com", "pidfpinfg#truformdftechnoproducts.com", "nthfsskare#ysahoo.in", "akashkatsdfsa#yahsdfsfoo.in", "bimsdaalprakash#live.com", "dfdmilifsd.ensfdfcogndfdfatia#gmail.com"]
Just an update to the accepted answer. This does not work for "plus" signs in the email address. GMAIL supports emailaddress+randomtext#gmail.com.
I've updated to:
return text.match(/([a-zA-Z0-9._+-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
The bellow function is RFC2822 compliant according to Regexr.com
ES5 :
var extract = function(value) {
var reg = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/g;
return value && value.match(reg);
}
ES6 :
const reg = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/g
const extract = value => value && value.match(reg)
Regexr community source
function GetEmailsFromString(input) {
var ret = [];
var email = /\"([^\"]+)\"\s+\<([^\>]+)\>/g
var match;
while (match = email.exec(input))
ret.push({'name':match[1], 'email':match[2]})
return ret;
}
var str = '"Name one" <foo#domain.com>, ..., "And so on" <andsoon#gmx.net>'
var emails = GetEmailsFromString(str)
Source
You don't need jQuery for that; JavaScript itself supports regexes built-in.
Have a look at Regular Expression for more info on using regex with JavaScript.
Other than that, I think you'll find the exact answer to your question somewhere else on Stack Overflow - How to find out emails and names out of a string in javascript
const = regex = /\S+[a-z0-9]#[a-z0-9\.]+/img
"hello sean#example.com how are you? do you know bob#example.com?".match(regex)
A bunch of the answer in here are including lower/capital letters [a-zA-Z] AND the insensitive regex flag i, which is nonsense.
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z]).
\d matches a digit (equivalent to [0-9])As domain extensions don't end with numeric characters).
As a result, combined with the \d token. we get a much more condenses and elegant sentence.
/[a-z\d._+-]+#[a-z\d._-]+/gi
Demo
let input = 'sdabhikagathara#rediffmail.com, "assdsdf" <dsfassdfhsdfarkal#gmail.com>, "rodnsdfald ferdfnson" <rfernsdfson#gmail.com>, "Affdmdol Gondfgale" <gyfanamosl#gmail.com>, "truform techno" <pidfpinfg#truformdftechnoproducts.com>, "NiTsdfeSh ThIdfsKaRe" <nthfsskare#ysahoo.in>, "akasdfsh kasdfstla" <akashkatsdfsa#yahsdfsfoo.in>, "Bisdsdfamal Prakaasdsh" <bimsdaalprakash#live.com>,; "milisdfsfnd ansdfasdfnsftwar" <dfdmilifsd.ensfdfcogndfdfatia#gmail.com>'
function get_email(string) {
return string.match(/[a-z\d._+-]+#[a-z\d._-]+/gi)
};
$('#output').html(get_email(input).join('; '));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="output"></div>
See it live # https://regex101.com/r/OveC5B/1/

validation of emails using jquery

i have data having emails:
data pattern is like: first_name last_name email
data = foo bar foo#bar.com, foo baz foo#baz.com,foo foo foo#foo.com,bar#bar.com , bar baz bar#baz.com
It may contain spaces. I have to valiadte all the emails by extracting the data.
Note: There may be spaces between words so that spliting by space gives me more some unwanted data ...
function validate() {
email_data = data.split(',') // This will get the all data spliting by ','
for (i = 0; i<email_data.length; i++) {
email_new_data = email_data.split(' ') //spliting data by space
// Now I could not find the way to extract emails (only ) from data
}
}
Any suggestion will be helpful.. thanks
The following function split the main data string into an array; which then can be parsed with RegExp. As far as I can tell you only want to get the email address; so, we use a Regular Expression to match an email address. If it matches, you have a valid email. If there's no match; then basically there's no valid email on the given segment.
Please note that Regular Expression 100% success Email Validation is just impossible; because the RFC is just extremely complicated, check more about here: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html
function validate() {
data = "foo bar foo#bar.com, foo baz foo#baz.com,foo foo foo#foo.com,bar#bar.com , bar baz bar#baz.com";
email_data = data.split(','); // This will get the all data spliting by ','
for (i = 0; i < email_data.length; i++) {
// Get emails, change console.log to alert if you don't know how to use console.
console.log(email_data[i].match(/[\w._+-]+#[\w._+-]+\.[\w]{2,6}/));
}
}
validate();
Working example:
http://jsfiddle.net/kuroir/H7VaT/
var email_list = new Array();
var k = 0;
for (j=0; i<email_new_data.length; i++) {
if (email_new_data.search('#') != -1) {
email_list[k++] = email_new_data;
}
}
Unless you have people with # in their name, it should work...
(This is in case you have people with space in their name/empty name string)
You need to clarify a bit more of what you want to do? Judging from your wording, you want to validate email addresses. Here's some untested code:
var emailpattern = new RegExp("^[a-zA-Z0-9._-]+#[a-zA-Z0-9-]+\.[a-z]{2,4}$");
function validateEmail(email){
return emailpattern.match(email);
}
function processEmail(emailData){
emailData = emailData.split(' ');
var firstName = emailData[0];
var lastName = emailData[1];
var email = emailData[2];
if (validateEmail(email)){
// Your stuff if valid
} else {
// If email is not valid
}
}
var data = data.split(','); // Whereever you get your data from
for (var i=0; i<data.length; i++){
processEmail(data[i]);
}
Can you validate this on the server instead?
The reason I ask is that many server-side libraries have pre-built libraries that do exactly this, implemented as a proper parser for the spec, not a RegEx.
Splitting the data and extracting the email is the easy part, validating the email is what will be hard.
See here: http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx

Categories

Resources