javascript - 使用 RegExp 过滤对象数组?在角 5-9
问题描述
我应该如何使用 RegExp 过滤对象(包括标题、电子邮件和姓名)
标题和名称只能使用字母。 电子邮件应该是有效的
正则表达式模式:
电子邮件 = /[a-zA-Z0-9.-]{1,}@[a-zA-Z.-]{2,}[.]{1}[a-zA-Z]{2,}/
标题和姓名 = /^[A-Za-z]+$/
如果任何一封电子邮件无效,则该无效电子邮件应存储在不同的变量中,而其他对象应存储在不同的变量中。
在 JSON 中,第一个对象电子邮件无效,所以我想将此对象存储在其他变量中
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 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)
.test()
API 运行搜索正则表达式和字符串之间的匹配项。
请在 javascript 中查找有关正则表达式匹配的以下文章,您可以找到几个有关正则表达式匹配的 API