How can I make a syntax highlighting HTML system for a text editor?
I thought of using a <pre>
tag with contenteditable="true"
, so using RegEx I would override the syntaxes of contentText, and would modify the innerHTML ...
But that does not work. Remembering that I want to make a system from scratch, and not use some script ready ... I just need a basic idea of how to do it.
Code:
<!DOCTYPE html>
<html>
<head>
<script>
function highlight(){
txt = document.getElementsByTagName("pre")[0].textContent;
txt = txt.replace(/ (abc|teste) /gi, "<span style='color:#F00'>teste</span>");
document.getElementsByTagName("pre")[0].innerHTML = txt;
}
</script>
</head>
<body>
<pre contenteditable="true" onkeyup="highlight();">
</pre>
</body>
</html>