在 JavaScript 中,我试图获取这两个字符串之间的所有内容,但是我想要获取的内容不在同一行中,而且我是从 api 获取的。
当我打电话时,我把它当作一个字符串
the 1960s with the release of Letraset sheets containing
Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker
including versions of Lorem Ipsum.
BlogPostStart
the 1960s with the release of L
etraset sheets containing Lorem Ipsum
passages, and more recently with desktop
publishing software like Aldus PageMaker
including versions of Lorem Ipsum.
BlogEnd
基本上我必须得到 BlogPostStart 和 BlogEnd 之间的所有内容,而不包括这两个词或顶部。
我试过这篇文章是来自api的整个字符串,基本上是什么:
var blogDescription = post.match(/^(?!\s*(BlogPost|EndBlog)).*/gmi);
但它只是以数组格式返回每一行。
您可以使用babel-plugin-transform-dotall-regexp
启用建议s
标志使点匹配所有内容,然后编写
/BlogPostStart(.*)BlogEnd/is
^ Use the new "s" flag.
您的结果将在捕获组中找到:
console.log(text.match(regexp)[1]);
在撰写本文时,该提案目前处于 TC39 流程的第 3 阶段,据我所知尚未在任何引擎中实施。XRegExp 包也支持它。
或者只是使用[^]
:
const text = `the 1960s with the release of Letraset sheets containing
Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker
including versions of Lorem Ipsum.
BlogPostStart
the 1960s with the release of L
etraset sheets containing Lorem Ipsum
passages, and more recently with desktop
publishing software like Aldus PageMaker
including versions of Lorem Ipsum.
BlogEnd`;
console.log(text.match(/BlogPostStart\s*([^]*)\s*BlogEnd/)[1]);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句