javascriptregexqregularexpression

how to match markdown block code between 3 grave accent


 ``` 
  block code 
  word1
  word2
 ```

word without grave accent and it is will match it too

 ```
  another block code
  word1
  word2
  - it's will match it as part of the first block code
 ```

"""
some text after the grave accent text
word1
word2
"""`
0: "``` \nblock code\nword1\nword2\n```\n\nword without grave accent and it's will match it too\n\n```\nanother block code\nword1\nword2\n- it's will match it as part of the first block code\n```"
length: 1
0: "``` \nblock code\nword1\nword2\n```"
1: "```\nanother block code\nword1\nword2\n- it's will match it as part of the first block code\n```"
length: 2

Solution

  • You'll want to lazily match your + quantifier: (\`{3}[\w|\W]+?\`{3})
    The +? will try to match one or more times with as few characters as possible, ensuring it doesn't greedily take up everything between your \`{3}s. Quantifiers are greedy by default.

    regex101 example

    Here's some more reading on lazy vs greedy.