javascript - 前瞻正则表达式

标签 javascript regex

我在 JavaScript 中有以下字符串,这是一个基本的 oData 批量请求:

--batchfull
Content-Type:  multipart/mixed; boundary=batchitems

--batchitems
Content-Type: application/http
Content-Transfer-Encoding: binary

MERGE /WcfDataService1.svc/Orders(1) HTTP/1.1
Content-Type: application/json;type=entry

{"OID":"2340324","PID":"234234"}

--batchitems
Content-Type: application/http
Content-Transfer-Encoding: binary

POST /WcfDataService1.svc/Users HTTP/1.1
Content-Type: application/json;type=entry

{"PID":"234234","Name":"User1"}

--batchitems--
--batchfull--

组成批处理的每个项目如下:

--batchitems
Content-Type: application/http
Content-Transfer-Encoding: binary

POST /WcfDataService1.svc/Users HTTP/1.1
Content-Type: application/json;type=entry

{"PID":"234234","Name":"User1"}
// new line here too

我正在尝试编写一个正则表达式来提取每个批处理项目,但我无法让它工作。到目前为止我已经:

/--batchitems$.*?(?=--batchitems)/ig

但是,它不起作用。

1)我尝试构建它,所以一开始我有/--batchitems$/ig。没有美元,它起作用,有了它,它不起作用(没有任何匹配)。

有什么想法可以让它发挥作用吗?

最佳答案

您可以使用 [\s\S]*? 而不是 .* ,因为 js 不支持 DOTALL 修饰符 s

/--batchitems\n([\s\S]*?)(?=--batchitems)/ig

DEMO

关于javascript - 前瞻正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32014316/

相关文章:

javascript - 当鼠标向下移动时滚动/拖动 div 中的值

ruby - 正则表达式匹配除结束字符串之外的任何内容

Javascript 正则表达式替换 URL 中的获取参数

c# - 检测到文本框有一个数字和一个大写字母

regex - 尽可能快地从众多正则表达式中选择正确正则表达式的算法

MYSQL:正则表达式替换

javascript - Sequelize : Issue with join table and querying for results

javascript - 根据选择标签下选择的选项使 div 隐藏和可见

javascript - 发送数据到下一页触发JS中的事件

php - 解析通过 javascript 填充内容的页面