0

I'm trying to match a regex in a text which contains line breaks and whitespaces on JavaScript. Here is my code:

$(document).ready(function(){
  var text = $('.main').text();
  var output = $('.output');
 
  var regex = /\*\*\*.*?\*\*\*.*?\*\-\*\*.*?\*\*\*/g;
  var matches = regex.exec(text);
  var i = 0;
  
  while (result = regex.exec(text)) {
    console.log(result);
  }
  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="main">
  <p>***{{1}}:Meaning***<br />
  ***{{2}}:Yep***<br />
  ***{{2}}:True***<br />
  <br />
  Lorem ipsum dolor sit amet
  <br />
  <br />
  *-**{{2}}:True***<br />
  *-**{{2}}:Yep***<br />
  *-**{{1}}:Meaning***<br />
  <br />
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
</div>

But with the 'g' flag it doesn't return the matches. I can get the matches with 's' because of line breaks.

I'm trying to get the expression between ***{{1}}:Meaning*** and *-**{{1}}:Meaning***. Firstly I used match() function, but it return only first match which is between ***{{1}}:Meaning*** and *-**{{2}}:True***. Then searched for it and find the exec() function which iterates.

I don't know how can I get the appropriate match. Here is the codepen.

EDIT

I'm editing, because it's hard and takes time to understrand the codes above.

My regex is:

/qqq.*?qqq.*?qwq.*?qqq/s

And text is:

qqqLOREMqqq
    Lorem ipsum dolor sit amet epictetus
    qqqANOTHERLOREMqqq
        Something something something
    qwqANOTHERLOREMqqq
    consectetur epilis
qwqLOREMqqq
Etiam ut felis molestie, tempor leo sit amet, pellentesque lorem. Sed consequat ut dui et mollis. Nulla sagittis vel felis id viverra. 

I want to get the text between qqqLOREMqqq and qwqLOREMqqq, but it takes qqqLOREMqqq and qwqANOTHERLOREMqqq.

sundowatch
  • 2,116
  • 3
  • 24
  • 35

0 Answers0