简体   繁体   English

如何在javascript中的单行中多次查找工作正则表达式模式?

[英]How to find working regex pattern multiple times in single line in javascript?

I want to validate an text input field with javascript regex validation.我想使用 javascript 正则表达式验证来验证文本输入字段。
Validation that is needed is: Input field should have one or multiple email ids, all ending with semicolon(;).需要的验证是:输入字段应该有一个或多个电子邮件 ID,都以分号 (;) 结尾。

Thus, correct input would be :因此,正确的输入将是:
→ user1@xxx.com; → user1@xxx.com;
→ user1@xxx.com;user2@xxx.com; → user1@xxx.com;user2@xxx.com;
→ user1@xxx.com;user2@xxx.com;user3@xxx.com; → user1@xxx.com;user2@xxx.com;user3@xxx.com;

Incorrect input would be不正确的输入将是
→ user1@xxx.com -------- Semicolon is missing → user1@xxx.com --------缺少分号
→ user1@xxx -------- Email Id is invalid → user1@xxx --------邮箱 ID 无效
→ user1@xxx.com;user2@xxx.com -------- Semicolon(;) is missing after second email id → user1@xxx.com;user2@xxx.com --------第二个电子邮件 ID 后缺少分号 (;)


This is what I have tried so far which validates only one occurence of regex pattern, but not all occurences in single line.这是我到目前为止所尝试的,它仅验证正则表达式模式的一次出现,但并非所有出现在单行中。

(^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(;)+$)


Regex URL: https://regex101.com/r/4svUQz/2/正则表达式网址: https : //regex101.com/r/4svUQz/2/
[All 4 values in TEST STRING should match, but it is matching only first 2] [TEST STRING 中的所有 4 个值都应该匹配,但它只匹配前 2 个值]

Also, I have checked below articles, but could not find answer.另外,我检查了以下文章,但找不到答案。
Regex single-line multiple match 正则表达式单行多匹配
Regex for The Same Pattern Multiple Times in One Line 在一行中多次使用相同模式的正则表达式
How to validate an email address in JavaScript 如何在 JavaScript 中验证电子邮件地址

First, for a simpler email regex to understand the principles involved, see How to check for a valid email address?首先,对于一个更简单的电子邮件正则表达式来理解所涉及的原则,请参阅如何检查有效的电子邮件地址? . . I will use: [^@]+@[^@]+\\.[^@]+ with the recommendation that we also exclude space characters and so, in your particular case requiring a semicolon at the end and allowing multiple email addresses on the line:我将使用: [^@]+@[^@]+\\.[^@]+并建议我们也排除空格字符,因此,在您的特定情况下,最后需要分号并允许多个电子邮件地址该行:

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

Note that I took the basic regex for an email address and appended ;请注意,我采用了电子邮件地址的基本正则表达式并附加了; and then put parentheses () around the entire expression and appended + signifying one or more times.然后在整个表达式周围加上括号()并附加+表示一次或多次。

See Regex Demo请参阅正则表达式演示

Using your regex, with a slight simplification involving the ;使用您的正则表达式,稍微简化一下; (ie removing unnecessary parentheses surrounding it): (即删除围绕它的不必要的括号):

^([a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+;)+$

See Regex Demo请参阅正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM