gpt4 book ai didi

javascript - 检查句子中是否存在日期字符串 (dd/mm/yyyy)?

转载 作者:行者123 更新时间:2023-11-28 17:12:10 25 4
gpt4 key购买 nike

如何使用正则表达式检查句子中是否存在日期字符串 (dd/mm/yyyy) ??

我使用的正则表达式如下。它没有找到任何匹配项。

var text = "...event held on 21/05/2018 at ...."
var match = text.match(/(\d{2})\/(\d{2})\/(\d{4})$/);

解决这个问题的有效方法是什么?

提前致谢!

最佳答案

$anchor point in a regex :

They do not match any character at all. Instead, they match a position before, after, or between characters. ... $ matches right after the last character in the string. c$ matches c in abc, while a$ does not match at all.

因此,您的正则表达式仅在字符串21/05/2018结尾时匹配。您可以将正则表达式简化为:

/\d{2}\/\d{2}\/\d{4}/

我删除了匹配组(大括号 ()),因为您并不隐式需要它们。 https://regexr.com/4686u

关于javascript - 检查句子中是否存在日期字符串 (dd/mm/yyyy)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54148539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com