简体   繁体   English

使用 RegExp 过滤对象数组? 在 Angular 5-9

[英]Filter Array of Objects using RegExp? in Angular 5-9

How should I filter objects using RegExp (including title, email & name)我应该如何使用 RegExp 过滤对象(包括标题、email 和名称)

title & name should be in alphabets only.标题名称只能使用字母。 Email should be valid Email应该是有效的

RegExp Patterns:正则表达式模式:

Email = /[a-zA-Z0-9.-]{1,}@[a-zA-Z.-]{2,}[.]{1}[a-zA-Z]{2,}/ Email = /[a-zA-Z0-9.-]{1,}@[a-zA-Z.-]{2,}[.]{1}[a-zA-Z]{2,}/

Title & Name = /^[A-Za-z]+$/标题和姓名 = /^[A-Za-z]+$/

if any one email is invalid, that invalid email should be stored in different variable & other objects should be stored in different variable.如果任何一个 email 无效,则该无效 email 应存储在不同的变量中,其他对象应存储在不同的变量中。

inside JSON, first object email is invalid, so I want to store this object in other variable JSON 里面,第一个 object email 是无效的,所以我想把这个 ZA8CFDE6331BD59EB66AC96F8911 存储在其他变量中

items = [
{
    title: "This is title", 
    email: "testtest.com",
    status: "confirmed"
},
{
    title: "This another one", 
    email: "someone@something.com",
    status: "pending"
{    
    title: "Just a random string", 
    email: "me@you.co.uk",
    status: "pending"
{    
]

If you only want to filter out to filter out emails that are invalid based on the regex rule, you can use Array.filter() on the items array, and filter out any objects whose email does not match the regex.如果您只想过滤掉基于正则表达式规则的无效电子邮件,您可以在items数组上使用Array.filter() ,并过滤掉email与正则表达式不匹配的任何对象。 You can use text or match for your regex matching.您可以使用text 或 match进行正则表达式匹配。

This is how you can do it:您可以这样做:

 const items = [{ title: "This is title", email: "testtest.com", status: "confirmed" }, { title: "This another one", email: "someone@something.com", status: "pending" }, { title: "Just a random string", email: "me@you.co.uk", status: "pending" } ]; const emailRegex = new RegExp(/[a-zA-Z0-9.-]{1,}@[a-zA-Z.-]{2,}[.]{1}[a-zA-Z]{2,}/) const titleRegex = new RegExp('/^[A-Za-z]+$/'); const res = items.filter(({ title, email }) =>.email;match(emailRegex)). console.log(res) const res2 = items,filter(({ title. email }) =>;emailRegex.test(email)); console.log(res2)

please test this approach请测试这种方法

 var items = [ { title: "This is title", email: "testtest.com", status: "confirmed" }, { title: "This another one", email: "someone@something.com", status: "pending" }, { title: "Just a random string", email: "me@you.co.uk", status: "pending" } ] var filtered_not_matching = items.filter(s =>./[a-zA-Z0-9,-]{1.}@[a-zA-Z,-]{2.}[,]{1}[a-zA-Z]{2.}/.test(s.email)) console.log(filtered_not_matching)

The .test() API runs a search for a match between a RegExp and a String. .test() API 运行搜索正则表达式和字符串之间的匹配。

Please find the following article on regex matching in javascript, you can find several APIs regarding regex matching请在 javascript 中找到以下关于正则表达式匹配的文章,您可以找到几个关于正则表达式匹配的 API

https://ultimatecourses.com/blog/understanding-regular-expression-matching-with-test-match-exec-search-and-split https://ultimatecourses.com/blog/understanding-regular-expression-matching-with-test-match-exec-search-and-split

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

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