Trying to use regex to turn markdown UL into HTML. Below, I've illustrated what an example input could look like, and the result should be two different ul
elements, the first having three li
elements and the second having two.
- Item 1
- Item 2
- Item 3
A second list:
- Item 1
- Item 2
Running into this infuriating issue where the following regex doesn't seem to be working as intended. The problem is that it doesn't seem to be recognising the \n
char, since the first regex /((- |ー).*(\n|$))+/g
seems to be only getting matches when there is end of string ($
).
.replaceAll(/((- |ー).*(\n|$))+/g, function(match) {
return `<ul>${match}</ul>`.replaceAll(/(- |ー).*/g, function(match) {
return `<li>${match.match(/(?<=(- |ー)).*/)}</li>`
});
});
I don't understand what the problem is, and I tested the expression in Regexr where it works perfectly.
Here is the full context if it would be helpful:
parse(markdown) {
return markdown
// Clean HTML brackets
.replaceAll('<', '<')
.replaceAll('>', '>')
// Change markdown links into html links
.replaceAll(/\[.*?\]\(.*?\)/g, function (match) {
return `<a href='${match.match(/(?<=\().*?(?=\))/)[0]}' target='_blank'>${match.match(/(?<=\[).*?(?=\])/)[0]}</a>`;
})
// Headings
.replaceAll(/(^|\n)# .*/g, function (match) {
return `<h1>${match.match(/(?<=# ).*/)}</h1>`
})
.replaceAll(/(^|\n)## .*/g, function (match) {
return `<h2>${match.match(/(?<=# ).*/)}</h2>`
})
.replaceAll(/(^|\n)### .*/g, function (match) {
return `<h3>${match.match(/(?<=# ).*/)}</h3>`
})
// Ordered lists
.replaceAll(/((- |ー).*($|\n))+/g, function(match) {
return `<ul>${match}</ul>`.replaceAll(/(- |ー).*/g, function(match) {
return `<li>${match.match(/(?<=(- |ー)).*/)}</li>`
});
});
Note that the \n
char is recognised perfectly fine in the // Headings
section.
(Edit to clarify that this is in VueJS, hence using this method definition syntax in a component's methods object)