2

Hey Stack Overflow members, I am trying to parse this text and get two separate matches, the unordered markdown list blocks. The problem is that i cannot figure out a way to match them. There maybe text after them.

I am using JavaScript flavored regex.

This is what i have been trying, full example: Regex101

(\*|\t\*|\s+\*).*

List:

* Item 1
* Item 2
    * Item 2a
    * Item 2b

* Item 1
* Item 2
    * Item 2a
    * Item 2b

Thank you in advance for your help.

0

1 Answer 1

1

[\s\S] will make JavaScript multiline, since dot . doesn't do multiline.

[\s\S] will search for every whitespace and non-whitespace character including newline characters.

var match = document.querySelector("pre").textContent.match(/(\*|\t\*|\s+\*)[\s\S]*?\n\n/g);

console.log(match);
<pre>

When there is text preceding the block

* Item 1
* Item 2
    * Item 2a
    * Item 2b

* Item 1
* Item 2
    * Item 2a
    * Item 2b
    
When there is more text in the block
</pre>

Another method. Use the global flag g for your match.

var match = document.querySelector("pre").textContent.match(/(\*|\t\*|\s+\*).*/g);

//raw match
console.log(match);

//with trim
match_trim = match.map(function(element){
    return (element.trim());
});

console.log(match_trim);

//with join
match_join = match.join("");

console.log(match_join);
<pre>
* Item 1
* Item 2
    * Item 2a
    * Item 2b

* Item 1
* Item 2
    * Item 2a
    * Item 2b
</pre>

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer the problem is that i want to match the groups separately, i am sorry if i was unclear updated example -> regex101.com/r/brjENQ/3
Well also update your question, because my solution is the answer to your question on this page. The following will definitely solve it: (\*|\t\*|\s+\*)[\s\S]*?\n\n. I advise method two though, for more control.
I checked your second method, and it will partially work , just need to remove the top white space. I think i can get it to work from here Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.