Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8401503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:49:24+00:00 2026-06-09T21:49:24+00:00

We’d like to allow several different formats for people to paste email addresses into

  • 0

We’d like to allow several different formats for people to paste email addresses into our app, those being:

1. foo@bar.com
2. "Foo Bar" <foo@bar.com>
3. Foo Bar <foo@bar.com>

Is there a good Javascript regular expression which can take any one of those 3 and give the email address, as well as the leading name if it exists? These seem to be the format users are copy/pasting into the app currently (we’re only supporting the first right now).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:49:26+00:00Added an answer on June 9, 2026 at 9:49 pm

    I think I might have got something! It can definitely be improved, but give me a moment to defend it before you start suggesting changes. Here’s the regex:

    /^(?:("{0,1})(\w+ \w+)\1 <([^@ ]+@[^@ ]+\.[^@. ]+)>|([^@ ]+@[^@ ]+\.[^@. ]+))$/
    

    Let me explain what it does. The first ^(?: says that the text must start at the beginning of the field and that the next group will not be captured in a backreference. We then start an optional item. This must be done since we want to require either 0 or 2 ", and 0 or 2 < marks. The first part of the optional item (before the | bar) deals with cases 2 and 3.

    Cases 2 and 3:

    The ("{0,1}) captures whether or not there is a quotation mark at the beginning. Then we use the(\w+ \w+) to require a first name and a second name, separated by a space, in cases 2 and 3. The parentheses around them capture them into a backreference so that you can get the value of the name later. Then comes the \1 – this is the backreference from the quotation marks earlier. It says that if you used quotation marks at the beginning of the name, you must use them at the end of the name too. Next up, the < says that the email address in cases 2 and 3 must be separated by a space from the name or ending quotation mark and must begin with a less-than sign. Finally, we have the regex for the email! This is the same for all cases. It is surrounded by parentheses to create a backreference. The [^@ ]+@ ensures that it must not have an @ symbol or space before the actual @ symbol. The following [^@ ]+\. does the same with the period after the @ symbol, and the trailing [^@. ]+ ensures the same is true after the period. We then have a closing parenthesis to end the backreference to the email address, and a > symbol because this is still for cases 2 and 3, which require one.

    Case 1:

    We then get to the | pipe, which specifies that we’ve gotten to the other option in the regex. If the format of the text did not match the opening conditions for cases 2 and 3, the regex checks to see if it’s just a plain email address (case 1). The regex here is the same as the one inbetween the <> symbols in cases 2 and 3 – it matches a valid email address and creates a backreference.

    Valid formats:

    Here’s an incomplete list of formats that work:

    foo@bar.com
    foo1.foo2@bar.com
    foo1.foo2@bar1.bar2.com
    Foo Bar <foo@bar.com>
    "Foo Bar" <foo@bar.com>
    foo bar <site1.site2@test1.test2.com>
    "foo bar" <site1.site2@test1.test2.com>
    

    WORKING DEMO

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.