I am trying to create a very simple text parser that would allow a contenteditable in HTML to parse Javascript. It somewhat works. However, it is all wonky when I type into the text section. It actually seems like it is writing backwards. It seems like it will parse the file fine otherwise... At least I believe so. Can someone take a look at the code below and let me know?
<section id="textarea" contenteditable="true" onkeypress="myFunction();">
<h2>Go ahead, edit away!</h2>
<p>Here's a typical paragraph element</p>
<ol>
<li>and now a list</li>
<li>with only</li>
<li>three items</li>
</ol>
</section>
<script>
var strReg1 = /"(.*?)"/g,
strReg2 = /'(.*?)'/g,
specialReg = /\b(new|var|if|do|function|while|switch|for|foreach|in|continue|break)(?=[^\w])/g,
specialJsGlobReg = /\b(document|window|Array|String|Object|Number|\$)(?=[^\w])/g,
specialJsReg = /\b(getElementsBy(TagName|ClassName|Name)|getElementById|typeof|instanceof)(?=[^\w])/g,
specialMethReg = /\b(indexOf|match|replace|toString|length)(?=[^\w])/g,
specialPhpReg = /\b(define|echo|print_r|var_dump)(?=[^\w])/g,
specialCommentReg = /(\/\*.*\*\/)/g,
inlineCommentReg = /(\/\/.*)/g;
var htmlTagReg = /(<[^\&]*>)/g;
function myFunction() {
var str = document.getElementById("textarea").innerHTML;
document.getElementById("textarea").innerHTML = str.replace(strReg1, '<font color="red">"$1"</font>');
document.getElementById("textarea").innerHTML = str.replace(strReg2,'<font color="green">"$1"</font>');
}
</script>