在数组中找到与给定的正则匹配的元素
我有一个字符串格式的不同元素的数组,我想检查数组中是否包含日期字段。目前我正在考虑使用日期正则表达式,然后将其与数组中的所有元素进行匹配,但我不确定如何使用它。
let arr = ['SUPERMARKET', '465', 'S', 'Arroyo', 'Pkwy', 'Pasadena,', 'CA', '91105', '1', '1', '1', 'DOZ', 'FREE', 'RANGE', 'EGG', '$3.89', 'GV', 'ORGANIC', 'MILK', '2%', '$4.19', 'SBUX', 'FRENCH', 'ROAST', 'WL', '$8.99', 'SUBTOTAL', '$17.07', 'TOTAL', '$17.07', '$17.07', 'PURCHASE', '$17.07', 'VISA', 'DEBIT', '3991', 'Auth', '#140987', 'Exp', 'Date', '**/**', 'Lane', '#14', 'Cashier', '1409', '11/21/2019', '01:28', 'PMRef/Seq', '#140987', 'MRCH', '204650', 'Term=001', 'IC=CC', 'EPS', 'Sequence', '934518', 'ITEM', '1', 'H,', 'STEPHIE', '11/21/2019', '01:28', 'PM', '4130', '29', '1041', '2675']
let dateRegex = "^([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2})$"
I am having an array of different elements in the string format, I wanted to check whether an array contains date field in it. Currently I am thinking of using a date regex and then match it with all the elements in an array, but I am not sure of how to use that.
let arr = ['SUPERMARKET', '465', 'S', 'Arroyo', 'Pkwy', 'Pasadena,', 'CA', '91105', '1', '1', '1', 'DOZ', 'FREE', 'RANGE', 'EGG', '$3.89', 'GV', 'ORGANIC', 'MILK', '2%', '$4.19', 'SBUX', 'FRENCH', 'ROAST', 'WL', '$8.99', 'SUBTOTAL', '$17.07', 'TOTAL', '$17.07', '$17.07', 'PURCHASE', '$17.07', 'VISA', 'DEBIT', '3991', 'Auth', '#140987', 'Exp', 'Date', '**/**', 'Lane', '#14', 'Cashier', '1409', '11/21/2019', '01:28', 'PMRef/Seq', '#140987', 'MRCH', '204650', 'Term=001', 'IC=CC', 'EPS', 'Sequence', '934518', 'ITEM', '1', 'H,', 'STEPHIE', '11/21/2019', '01:28', 'PM', '4130', '29', '1041', '2675']
let dateRegex = "^([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2})quot;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试一下:
注意:
mm/dd/yyyy
,而您使用的正格是dd/mm/yyyy 因此,
dd
您的日期('21')中的部分与正则匹配。我已经更改了此答案中的日期以匹配正则态度。^([0]?[1-9] | [1] [0-2] [0-2 ])[./-]([0]?[1-9] | [1 | 2] [0-9] | [3] [0 | 1])[./-]([0-9] { 4} | [0-9] {2})$
/
之间,而不是引号“
You can try this:
Note:
mm/dd/yyyy
, while the regex you use is of the formatdd/mm/yyyy
so thedd
part in your dates('21') wont match the regex. I have changed the dates in this answer to match the regex.mm/dd/yyyy
), by using:^([0]?[1-9]|[1][0-2])[./-]([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0-9]{4}|[0-9]{2})$
/
instead of quotes"
这将返回满足
X.Match(WhateEverRegex)
的每个元素this will return every element that satisfies
x.match(whateEverRegex)