<script> var DataTypes = ["abstract", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "debugger", "default", "delete", "do", "double", "else", "enum", "export", "extends", "false", "final", "finally", "float", "for", "function", "goto", "if", "implements", "import", "in", "instanceof", "int", "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "typeof", "var", "void", "volatile", "while", "with", "length", "random", "Math", "for("]; for(var t = 0 ; t < DataTypes.length ; ++t){ var type = DataTypes[t]; //var color = "#" + Math.random().toString(16).slice(2, 8); //if()color="#c70183"; //else color="#000000"; code = document.getElementById("code").innerHTML; //wez <p> element z kodem var matches = getMatches(type, code); //wez indeksy pierwszych pojawien sie wyrazu int w tekscie for(var i = matches.length-1 ; i >= 0 ; --i){ // przelec wszystkie pojawienia sie od tylu. Od tylu, bo jak dodasz do tekstu "<span style='color:red'>" to sie tekst wydluzy i te indeksy beda juz nieaktualne var m = matches[i]; // indeks pojawienia sie int w tekscie code = spliceSplit(code, m+type.length, 0, "</b></span>") // ta funkcja dodaje tekst w okreslonym indeksie, paramtry ( w jakim tekscie, na ktorym indeksie, to niewazne, co dodac) if (type=='for' || type=='var' || type=='length' || type=='function' || type=='return' || type=='int' || type=='boolean' || type=='short' || type=='long' || type=='return') color="#800000"; else color="#000000"; code = spliceSplit(code, m, 0, "<span style='color:"+color+"'><b>") //console.log(code); } document.getElementById("code").innerHTML = code; //podmien caly tekst w htmlu zmienionym tekstem } function spliceSplit(str, index, count, add) { var ar = str.split(''); ar.splice(index, count, add); return ar.join(''); } function getMatches(needle, haystack) { var myRe = new RegExp("\\b" + needle + "\\b((?!\\W(?=\\w))|(?=\\s))", "gi"), myArray, myResult = []; while ((myArray = myRe.exec(haystack)) !== null) { myResult.push(myArray.index); } return myResult; } </script>