Regular expression for the validation of emails using a delimiter

Can someone help me create a regex expression for emails? This is what I have so far:

\S+@\S+\.\S+

I want to repeat this X times with a ; separator. Any ideas on how to achieve this? For example, the pattern should allow strings like:

<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b7e637a766b777e2a5b7e767a727735787476">[email protected]</a>
<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2d7cad3dfc2ded783f2d7dfd3dbde9cd1dddf">[email protected]</a>;<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f09588919d809c95c2b0959d91999cde939f9d">[email protected]</a>;<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f5a475e524f535a0c7f5a525e5653115c5052">[email protected]</a>;<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="67021f060a170b025327020a060e0b4904080a">[email protected]</a>

But it shouldn't allow strings like:

<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5c39243d312c30396d1c39313d3530723f3331">[email protected]</a>;example2email.com;<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e6b766f637e626b3d4e6b636f6762206d6163">[email protected]</a>;

Answer №1

Typically, the list of entities separated by X is checked against a pattern like ^y(?:Xy)*$.

However, there's a caveat regarding what matches with y and X: \S includes the ; character, so to exclude it, replace \S with [^\s;] in the final pattern.

You could utilize this expression:

^[^\s;]+@[^\s;]+\.[^\s;]+(?:;[^\s;]+@[^\s;]+\.[^\s;]+)*$

For more information, check out the regex demo.

Explanation:

  • ^ - denotes the start of the string
  • [^\s;]+@[^\s;]+\.[^\s;]+ - sequence of characters excluding whitespace and ;, followed by an @, another sequence without whitespace or ;, a dot, and once again a sequence without whitespace or ;
  • (?: - beginning of a non-capturing group:
    • ; - semi-colon
    • [^\s;]+@[^\s;]+\.[^\s;]+ - email-like structure (as described above)
  • )* - end of the non-capturing group, allowing for zero or multiple occurrences
  • $ - marks the end of the string

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The RC-dock library's 'DockLayout' is not compatible with JSX components. The instance type 'DockLayout' is not a valid JSX element and cannot be used as such

Despite encountering similar questions, none of the provided answers seem to address the issue within my codebase. My project utilizes React 17, Mui v5, and TS v4. I attempted to integrate a basic component from an external package called rc-dock. I simply ...

Is Validators.required a necessity in Angular 2?

Is there a way to dynamically require a form field based on conditions? I created a custom validator, but the conditional variables passed to it remain static. How can I update these conditional values within the custom validator function? Is it possible t ...

I am encountering challenges with React.js implemented in Typescript

Currently, I'm grappling with a challenge while establishing a design system in ReactJS utilizing TypeScript. The issue at hand pertains to correctly passing and returning types for my components. To address this, here are the steps I've taken so ...

Nexus and GraphQL: The root typing path for the "context" type is not found

I’m currently working on integrating GraphQL into Next.js API routes. For writing the GraphQL schema, I’m utilizing Nexus. Here are the two essential files: context.ts and schema.ts, that help in setting up Nexus development mode. // context.ts import ...

How can I effectively test static navigationOptions using Jest and Enzyme in a React Navigation and TypeScript environment?

Currently, I am developing a React Native app using TypeScript. For component testing, I rely on Jest and Enzyme. Additionally, I have integrated React Navigation into my project. On one of the screens, the navigationOptions are as follows: static naviga ...

Is Angular 9's default support for optional chaining in Internet Explorer possible without the need for polyfill (core-js) modifications with Typescript 3.8.3?

We are in the process of upgrading our system to angular 9.1.1, which now includes Typescript 3.8.3. The @angular-devkit/[email protected] utilizing [email protected]. We are interested in implementing the optional chaining feature in Typescript ...

Breaking apart text data into separate parts with pandas

https://i.sstatic.net/DuQSE.png I attempted to achieve my desired outcome using the code below: df1['company_etrim']=df1['company_trim'].str.replace(r'[0-9()%]', "").str.join('') However, the result displayed in th ...

Angular4 allows for the creation of a div that can horizontally scroll

Below is the HTML code I am working with: <div class="card-deck" fxLayout.xs="row" style="overflow: scroll; height:100%"> <md-card style="width:10rem" *ngFor="let make of filteredMakes" (click)="goToModels(make.niceName)" class=" ...

Changing an element within an item stored in Ionic Storage

Hello, I am currently attempting to update a specific part of an object stored in Ionic storage. The current data in the Storage looks like this: key : object value : {a: "1", b: "2", c: "3"} To modify one of the values to 10, I created the following fu ...

Pattern to identify a JSON string with Regular Expressions

Currently, I am working on developing a JSON validator from the ground up and have hit a roadblock when it comes to the string component. My original plan was to create a regex pattern that aligns with the sequence specified on JSON.org: Here is the regex ...

What is the proper way to type a collection and put it into action?

I am looking for a way to create an object that mimics a set. Specifically, I want the transaction id to act as a key and the transaction details as the value. To achieve this, I created the following: type TransactionDetail = { [key: TransactionId]: Tra ...

Error thrown by webpack: Module 'pug' not found when attempting to access get-api

After setting up webpack in express, a new folder was created. When I try to run bundle.js, it shows the message "server is running on port 3000". However, when I access the API at http://localhost:3000/api/test, the whole bundle.js loads in the console an ...

Retrieve a multitude of nested Records within the returnType that correlate with the number of arguments provided to the function

My goal is to dynamically define a deeply nested ReturnType for a function based on the number of arguments passed in the "rest" parameter. For example, if we have: getFormattedDates( dates: Date[], ...rest: string[] // ['AAA', 'BBB&apos ...

Using TypeScript with React may result in an error message stating that a string cannot be assigned to a parameter of type 'keyof T'

I've encountered an issue with my sorting function that I built using TypeScript and React Hooks. The error message I'm getting is: Argument of type 'string' is not assignable to parameter of type 'keyof T'. Type 'stri ...

Utilizing the Solana Wallet Key for ArweaveJS Transaction Signing

Is there a method to import a Solana wallet keypair into the JWKInterface according to the node_modules/arweave/node/lib/wallet.d.ts, and then generate an Arweave transaction using await arweave.createTransaction({ data }, jwk);? Metaplex utilizes an API ...

Serious issue: a dependency request is an expression (Warning from Angular CLI)

I am currently exploring the dynamic loading of lazy child routes within a lazy routing module. For example: const serverResponse = [ { path: "transaction", children: [ { path: "finance", modulePath: &qu ...

Apply criteria to an array based on multiple attribute conditions

Given an array containing parent-child relationships and their corresponding expenses, the task is to filter the list based on parents that have a mix of positive and negative expenses across their children. Parents with only positive or negative child exp ...

Is there a way to deactivate the click function in ngx-quill editor for angular when it is empty?

In the following ngx-quill editor, users can input text that will be displayed when a click button is pressed. However, there is an issue I am currently facing: I am able to click the button even if no text has been entered, and this behavior continues li ...

Tips for creating animations using parent and child components in Angular

Despite my best efforts, it seems like this should be functioning properly... but unfortunately it's not... I'm attempting to achieve a transition effect on the parent element (ui-switch-groove) while the child element (ui-switch-dongle) moves. ...

Using Angular to declare a variable for reuse within nested HTML elements

Exploring the realm of angular development has sparked my interest, however, I found myself at a roadblock while reading through the documentation. The issue lies in figuring out how to declare a variable that can be reused effectively within nested HTML e ...