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

  • SEARCH
  • Home
  • 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 8166597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:55:46+00:00 2026-06-06T19:55:46+00:00

I am using this regex to match email addresses in a string. Everything works

  • 0

I am using this regex to match email addresses in a string.

Everything works fine here: http://regexr.com?31e5a
with this regex:

([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})

But when I am trying to use it with javascript, I am getting only the first email address, not all of them.

Here is my code:

var emailsString = 'aaaaaaa@bbbb.com xxxxxxx cccccc@dddd.com';
var emails = emailsString.match(/([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})/)[0];

Ad here, I am getting only the first email.

If you can help me, thank you.

  • 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-06T19:55:49+00:00Added an answer on June 6, 2026 at 7:55 pm

    You need to add a ‘g’ modifier:

    var emailsString = 'aaaaaaa@bbbb.com xxxxxxx cccccc@dddd.com';## Heading ##
    var emails = emailsString.match(/([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})/g)
    

    Also, as mentioned elsewhere in this post, your regex does not match all valid emails. Matching valid emails with regex is actually a pretty difficult problem, but to start with, the TLD component can be longer than 4 characters so you should adjust that accordingly.

    My recommendation is to use the other regex mentioned here in combination with the /g modifier to get all matches.

    The g modifier is used to perform a global match (find all matches
    rather than stopping after the first match).

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

Sidebar

Related Questions

I'm using @diegoperini's regex to validate URL's, However this: http://domain.com/#foo -> Matches http://domain.com#foo ->
I'm currently using this regex in my php script to validate email addresses: preg_match_all(/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i,
I am using this regex: \b(((\S+)?)(@|mailto\:|(news|(ht|f)tp(s?))\://)\S+)\b to match this string of text (yes, it
I have this regex for matching email addresses in a string. [a-zA-Z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-zA-Z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.([a-zA-Z]{2,}) It is
Im parsing the source of a website and Im using this regex: /page\.php\?id\=([0-9]*)\\>(.*)\<\/a\>\<\/span\>/.match(self.agent.page.content) self.agent.page.content
Hi have this URL string which I need to extract possibly using regex but
I am using this regex: preg_match('~[^A-Za-z0-9\+\-]~', $incoming); How can I add empty spaces here?
String.prototype.is_email = function() { return this.match(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)b/); }; I'm trying to get all my javascript
I am using this RegEx to match a word before a colon. ([^\s]+)\: However,
I'm using this jquery plugin: https://github.com/maranomynet/linkify/blob/master/1.0/jquery.linkify-1.0.js With this REGEX plugins I'm trying to linkify

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.