在不在同一行的两个字符串之间获取字符串,正则表达式

时间:2017-05-02 01:57:12

标签: javascript regex

在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);

但它只返回数组格式的每一行。

2 个答案:

答案 0 :(得分:2)

不,React对此没有任何帮助。它只是普通的旧JavaScript ......当然还有正则表达式=)



var string = "the 1960s with the release of Letraset sheets containing <br/>\
Lorem Ipsum passages, and more recently with desktop <br/>\
publishing software like Aldus PageMaker <br/>\
including versions of Lorem Ipsum. <br/>\
<br/>\
BlogPostStart \
\
the 1960s with the release of L <br/>\
etraset sheets containing Lorem Ipsum<br/>\
passages, and more recently with desktop<br/>\
publishing software like Aldus PageMaker <br/>\
including versions of Lorem Ipsum.<br/><br/>\
Mussum Ipsum, cacilds vidis litro abertis. <br/>\
Nullam volutpat risus nec leo commodo, ut interdum diam laoreet. Sed non consequat odio. Quem manda na minha terra sou euzis! Paisis, filhis, espiritis santis. Viva Forevis aptent taciti sociosqu ad litora torquent.<br/>\
<br/>\
BlogEnd";


// REGEX ===============================================================
// =====================================================================
var result = string.match(/(?:BlogPostStart)([\s\S]*)(?:BlogEnd)/)
// =====================================================================
// =====================================================================

document.write(!!result && result[1].trim());
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以使用babel-plugin-transform-dotall-regexp启用建议的s标记,使点匹配所有内容,然后编写

/BlogPostStart(.*)BlogEnd/is
                           ^ Use the new "s" flag.

您的结果将在捕获组中找到:

console.log(text.match(regexp)[1]);

在撰写本文时,proposal目前处于TC39流程的第3阶段,据我所知还没有在任何引擎中实现。它也受XRegExp package支持。

或者只使用[^]

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]);