I am developing a rich text editor for my website. If the user wrote something that has HTML syntax, I would like it to convert it to HTML, just like the text editor in Stack Overflow.
I would like it to:
- split the text on each tag, and the array elements should include the tag that was written
- transform the
<
and>
to their corresponding signs, unless the tags are inside PRE and CODE tags
For now, I tried using a Regexp I found here for splitting the HTML, but if I test the code below, I would get:
['Lorem ipsum dolor', 'sit amet', 'consectetur', 'adipiscing', 'elit.' 'Sed erat odio, fringilla in lorem eu.']
, which is defintely not what I want, I would want something like:
['Lorem ipsum dolor', '<h1>', 'sit amet', '</h1>', '<h6>', 'consectetur', '<b>', 'adipiscing', '</b>, '</h6>', 'elit.', '<br>', 'Sed erat odio, fringilla in lorem eu.']
Then I would just:
function splitHTML(str) {
return str.split(/<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g)
}
function isHTML(str) {
return /<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g.match(str)
}
const arr = splitHTML("Lorem ipsum dolor <h1>sit amet</h1>, <h6>consectetur <b>adipiscing</b> </h6>elit. <br>Sed erat odio, fringilla in lorem eu.")
for (let element of arr) {
if (isHTML(element)) {
element = cod.replaceAll('<', '<');
element = cod.replaceAll('>', '>');
}
}
arr.join()
My question is:
How to split a text including the separator in the result.
And I also would like to know how to check if the code is between pre
and code
tags.
', 'sit amet', '
', '', 'consecte', '', 'tur', '', 'adipiscing', '
', 'elit.', '', 'Sed erat odio, fringilla in lorem eu.'] so then when it's done I can check using the same regexp but with match if it is html and the replace all. – PoliPau Dec 18 '20 at 20:41