Javascript 正则表达式组结果和/或
是否可以返回包含两个重要正则表达式检查的数组 类似这样的东西-Javascript 正则表达式组结果和/或,javascript,regex,Javascript,Regex,是否可以返回包含两个重要正则表达式检查的数组 类似这样的东西-/(?=.*\d天)(?=.*汽车|卡车|摩托车)/ 第一个问题是正则表达式与汽车或其他车辆不匹配。。。 第二,是否可以将天数…*\d天(…提取到一个单独的数组值中 目前我的正则表达式是-/(?=.*d天)(?=.*car)/ 响应为-['',索引:0,输入:“洗车前5天”] 我正在争取-['5',索引:0,输入:“洗车前5天”] 投入 5 days before cleaning a car Motorcycle cleaning
/(?=.*\d天)(?=.*汽车|卡车|摩托车)/
第一个问题是正则表达式与汽车或其他车辆不匹配。。。
第二,是否可以将天数…*\d天(…
提取到一个单独的数组值中
目前我的正则表达式是-/(?=.*d天)(?=.*car)/
响应为-['',索引:0,输入:“洗车前5天”]
我正在争取-['5',索引:0,输入:“洗车前5天”]
投入
5 days before cleaning a car
Motorcycle cleaning in 23 days
truck will arrive in 10 days
基本上,我想在短文本中获得可能存在的任何车辆的天数。Description
此正则表达式将执行以下操作:
- 捕获天数
- 捕获车辆类型
5 days before cleaning a car
Motorcycle cleaning in 23 days
truck will arrive in 10 days
样本匹配
[0][0] = 5 days before cleaning a car
[0][1] = 5
[0][2] = car
[1][0] = Motorcycle cleaning in 23 days
[1][1] = 23
[1][2] = Motorcycle
[2][0] = truck will arrive in 10 days
[2][1] = 10
[2][2] = truck
解释
输入是什么样子的?!更新了问题以包含潜在输入和我期望得到的内容。您的目标只是捕获天数?是的。我只想提取该值…您的表达式非常接近。我将
\d
更改为([0-9]+)
这样它将捕获多个数字。我将汽车|卡车|摩托车
更改为(汽车|卡车|摩托车)
因此它们将被捕获…从技术上讲,车辆类型可以以任何顺序出现,但为了提高效率,它们应该从最常见到最不常见进行排序。然后,我在末尾添加了一个*
,以便捕获组0将获得整个字符串…以防万一您想在代码的其他地方对其进行进一步分析。D你是自己写解释画的还是有一些有用的工具我不知道这会自动地画出来吗?哇!哇!好答案!谢谢你的解释!你是怎么做的?你能推荐一本关于regex的好书或资源,帮助我学习它。拥有一个非常迷人的技能!我从中学习了正则表达式。该网站非常详细,并准确地解释了引擎的工作原理。但是,这不是一个简单易读的计划,计划在每个子页面上花费几天时间。关于图表,我使用了。除了一个经常使用的用户之外,我与这两个网站都没有任何关联。谢谢,伙计!你的答案是一个很好的资源。我将学习编辑并浏览你发布的链接。太棒了!
[0][0] = 5 days before cleaning a car
[0][1] = 5
[0][2] = car
[1][0] = Motorcycle cleaning in 23 days
[1][1] = 23
[1][2] = Motorcycle
[2][0] = truck will arrive in 10 days
[2][1] = 10
[2][2] = truck
NODE EXPLANATION
----------------------------------------------------------------------
^ the beginning of a "line"
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
----------------------------------------------------------------------
days 'days'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
motorcycle 'motorcycle'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
truck 'truck'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
car 'car'
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------