使用Lookahead在多行上匹配相似模式的JavaScript
我正在尝试使用正则表达式块,它将使用javascript从cucumber示例中提取表。黄瓜样品如下使用Lookahead在多行上匹配相似模式的JavaScript,javascript,regex,Javascript,Regex,我正在尝试使用正则表达式块,它将使用javascript从cucumber示例中提取表。黄瓜样品如下 Feature: Sample Feature File Scenario: An international coffee shop must handle currencies Given the price list for an international coffee shop | product | currency | price | |
Feature: Sample Feature File
Scenario: An international coffee shop must handle currencies
Given the price list for an international coffee shop
| product | currency | price |
| coffee | EUR | 1 |
| donut | SEK | 18 |
When I buy 1 coffee and 1 donut
Then should I pay 1 EUR and 18 SEK
Scenario Outline: eating
Given there are <start> cucumbers
When I eat <eat> cucumbers
Then I should have <left> cucumbers
Examples:
| start | eat | left |
| 12 | 5 | 7 |
| 20 | 5 | 15 |
二,
一旦我得到了块,我将按行分割以得到表中的行数。无论如何,为了解决这个问题,我尝试了一个否定的查找表达式。我的努力不足
/(\|)[\s\S]*\|(?!\s+\|)/gm
然而,这又回来了
| product | currency | price |
| coffee | EUR | 1 |
| donut | SEK | 18 |
When I buy 1 coffee and 1 donut
Then should I pay 1 EUR and 18 SEK
Scenario Outline: eating
Given there are <start> cucumbers
When I eat <eat> cucumbers
Then I should have <left> cucumbers
Examples:
| start | eat | left |
| 12 | 5 | 7 |
| 20 | 5 | 15 |
关于我的正则表达式哪里出了问题,有什么建议吗?非常感谢。该[\s\s]*模式尽可能多地匹配任何0+字符,直到字符串中没有1+空格且紧靠当前位置右侧的最后一个字符。由于从左到右搜索匹配项,因此获得单个匹配项是合乎逻辑的
我建议像这样展开图案
/^[^\S\r\n]*\|.*\|(?:[^\S\r\n]*[\r\n]+[^\S\r\n]*\|.*\|)*/gm
看
注意:如果动态构建,则可以使其可读:
var h=[^\\S\r\n]*;//水平空白块
var rx=new RegExp^+//行的开头
h+\\\\\\\\\\+//hor。空格、|、除换行符以外的0+字符|
?:+h++[\r\n]++//0+hor序列。空格,换行符,
h+\\\\\\\\*,//hor。空格、|、除换行符以外的0+字符|
总经理;//全局查找多个匹配项和多行^matches行开始
var s=Feature:示例功能文件\r\n\r\n场景:国际咖啡店必须处理货币\r\n给定国际咖啡店的价目表\r\n |产品|货币|价格| \r\n |咖啡|欧元| 1 | \r\n |甜甜圈|瑞典克朗| \r\n当我购买1杯咖啡和1个甜甜圈时\r\n我应该支付1欧元和18瑞典克朗吗\r\n\r\n场景概述:吃\r\n如果有黄瓜\r\n当我吃黄瓜时\r\n那么我应该吃黄瓜\r\n\r\n示例:\r\n |开始|吃|左| \r\n | 12 | 5 | 7 | \r\n | 20 | 5 | 15 |;
console.logs.matchrx;?:\s*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\+?几乎该表达式的两边都包含空格,但距离较近,即它有上一行和下一行的空格,后面是。或者多一点。
| product | currency | price |
| coffee | EUR | 1 |
| donut | SEK | 18 |
When I buy 1 coffee and 1 donut
Then should I pay 1 EUR and 18 SEK
Scenario Outline: eating
Given there are <start> cucumbers
When I eat <eat> cucumbers
Then I should have <left> cucumbers
Examples:
| start | eat | left |
| 12 | 5 | 7 |
| 20 | 5 | 15 |
| product | currency | price |
| coffee | EUR | 1 |
| donut | SEK | 18 |
/^[^\S\r\n]*\|.*\|(?:[^\S\r\n]*[\r\n]+[^\S\r\n]*\|.*\|)*/gm