• Jetzt anmelden. Es dauert nur 2 Minuten und ist kostenlos!

Javascript: Fragen zu replace()

Mordred

Neues Mitglied
Hallo!
Ich habe eine Frage zur Funktion "replace":

Wie kann ich die Funktion so anwenden, dass sie Text nicht komplett ersetzt, sondern nur am Anfang und am Ende des Gesuchten weiteren Text einfügt?

Danke im Voraus : )
 
Ich habe eine (kostenlose) Webseite mit einem Tutorial für "Lua - The Programming Language".
Die Skriptboxen sollten ihren Inhalt automatisch einfärben, aber auf der (kostenlosen...) Seite gibt es kein PHP.
So sieht es bisher aus:
Code:
<script type="text/javascript" >
   pretags = document.getElementsByTagName("pre");
    for (var i in pretags) {
     if (typeof(pretags[i]) == "object") {
      if (pretags[i]["className"] == "lua") {
       var ersetz = new Array("Hal","lo");
        //LuaBlau
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(do)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(end)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(for)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/( in )/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(if)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(then)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(else)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(while)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(repeat)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(until )/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(function)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(return)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(brake)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(local )/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(true)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(false)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(nil)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/( and )/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/( or )/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(not)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(xxxxxx)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(xxxxxx)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(xxxxxx)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(xxxxxx)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(xxxxxx)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(xxxxxx)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(xxxxxx)/g,"<lua_blue>$1</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(xxxxxx)/g,"<lua_blue>$1</lua_blue>");
        //LuaTurkis
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(_G)/g,"<lua_turq>$1</lua_turq>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(_VERSION)/g,"<lua_turq>$1</lua_turq>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(assert)/g,"<lua_turq>$1</lua_turq>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(collectgarbage)/g,"<lua_turq>$1</lua_turq>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(setmetatable)/g,"<lua_turq>$1</lua_turq>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(getmetatable)/g,"<lua_turq>$1</lua_turq>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(error)/g,"<lua_turq>$1</lua_turq>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(gcinfo)/g,"<lua_turq>$1</lua_turq>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(ipairs)/g,"<lua_turq>$1</lua_turq>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(tonumber)/g,"<lua_turq>$1</lua_turq>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(tostring)/g,"<lua_turq>$1</lua_turq>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(type)/g,"<lua_turq>$1</lua_turq>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(unpack)/g,"<lua_turq>$1</lua_turq>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(xpcall)/g,"<lua_turq>$1</lua_turq>");
        //LuaLila
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(string.byte)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(string.char)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(string.dump)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(string.find)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(string.format)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(string.gfind)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(string.gmatch)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(string.gsub)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(string.len)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(string.lower)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(string.match)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(string.rep)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(string.reverse)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(string.sub)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(string.upper)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(table.concat)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(table.foreach)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(table.foreachi)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(table.getn)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(table.insert)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(table.remove)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(table.setn)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(table.sort)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.abs)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.acos)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.asin)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.ceil)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.cos)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.deg)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.exp)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.floor)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.frexp)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.ldexp)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.log)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.log10)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.max)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.min)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.mod)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.pi)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.rad)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.random)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.randomseed)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.sin)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.sqrt)/g,"<lua_lila>$1</lua_lila>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(math.tan)/g,"<lua_lila>$1</lua_lila>");
        //Extra
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/--(.+)/g,"<lua_comment>--$1</lua_comment>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(/(\d+)/g,"<lua_number>$1</lua_number>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace("(","<lua_blue>(</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace(")","<lua_blue>)</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace("{","<lua_blue>{</lua_blue>");
       pretags[i].innerHTML = pretags[i].innerHTML.replace("}","<lua_blue>}</lua_blue>");
      }
     }
    }
  </script>
 
Hab ich nicht getestet, kann es dir also auch nicht sagen, aber der Code sieht dann übersichtlicher aus und erschlägt einen nicht, wenn man ihn nach einiger Zeit wieder editieren will.
 
Zurück
Oben