2017-05-02 4 views
2

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

하지만 그것은 단지 배열 형식으로 모든 행을 반환합니다.

+1

겠습니까 뭔가 : http://regexr.com/3fs8m – jas7457

답변

0

물론 정규식 =)로, 다음 쓰기의

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

이 작품 같은
2

아니요, React에는 아무 것도 없습니다. 그냥 평범한 구식 자바 스크립트 ... 당신은 도트 경기 모두를 만들기 위해 제안 된 s 플래그를 활성화하는 babel-plugin-transform-dotall-regexp을 사용할 수 있습니다

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

관련 문제