SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    replace(re,"<font color=red>"+re+"</font>")

    if Number is not red, Number will be red.
    if 1 is not red, 1 will be red.
    if 2 is not red, 2 will be red.
    How can I do this? Thanks...
    Code:
      
    <script type="text/javascript">
    
    window.onload = function () {
    
    var el = document.getElementsByTagName('pre')[0];
    el.style.fontSize ="16px";
    el.style.backgroundColor ="turquoise";
    el.innerHTML = el.innerHTML.replace(/var/g,"<font color=red>var</font>") ;
    el.innerHTML = el.innerHTML.replace(/&lt;/g,"<font color=blue><</font>") ;
    el.innerHTML = el.innerHTML.replace(/&gt;/g,"<font color=blue>></font>") ;
    el.innerHTML = el.innerHTML.replace(/'/g,"<font color=skyblue>'</font>") ;
    el.innerHTML = el.innerHTML.replace(/\(/g,"<font color=purple>(</font>") ;
    el.innerHTML = el.innerHTML.replace(/\)/g,"<font color=purple>)</font>") ;
    el.innerHTML = el.innerHTML.replace(/\{/g,"<font color=deeppink>{</font>") ;
    el.innerHTML = el.innerHTML.replace(/\}/g,"<font color=deeppink>}</font>") ;
    el.innerHTML = el.innerHTML.replace(/function/g,"<font color=yellow>function</font>") ;
    el.innerHTML = el.innerHTML.replace(/el/g,"<font color=white>el</font>") ;
    var re = /\d/g
    el.innerHTML = el.innerHTML.replace(re,"<font color=red>"+re+"</font>") ;
    }
    
    </script>
    <body>
    
    <pre>
    &lt;script type="text/javascript"&gt;
    
    window.onload = function () {
    
    var el = document.getElementsByTagName('pre')[0];
    el.innerHTML = el.innerHTML.replace("var","<font color=red>var</font>") ;
    var a = 5 ;
    }
    
    &lt;/script&gt;
    
    if Number is not red, Number will be red.
    if 1 is not red, 1 will be red.
    if 2 is not red, 2 will be red.
    Sayılar kırmızı değilse kırmızı olacak.
    1 kırmızı değilse kırmızı olacak.
    2 kırmızı değilse kırmızı olacak.
    </pre>
    
    </body>

  2. #2
    SitePoint Zealot the DtTvB's Avatar
    Join Date
    Jul 2006
    Location
    Thailand
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var re = /(\d)/g
    el.innerHTML = el.innerHTML.replace(re,"<font color=red>$1</font>") ;

  3. #3
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the DtTvB Thanks...
    Your code work very well on Firefox and Internet Explorer.

    When I did not add
    el.innerHTML = el.innerHTML.replace(/(\d)/g,"<font color=red>$1</font>") ;

    function is blue.
    for is deeppink.
    ' is lime.
    { and } is deeppink.
    // el.innerHTML = el.innerHTML.replace(/(\d)/g,"<font color=red>$1</font>") ;

    on Opera
    Code:
     
    <script type="text/javascript">
    
    window.onload = function () {
    
    var el = document.getElementsByTagName('pre')[0];
    el.style.fontSize ="16px";
    // el.style.backgroundColor ="aqua";
    
    el.innerHTML = el.innerHTML.replace(/&lt;/g,"<font color=blue><b><</b></font>") ;
    el.innerHTML = el.innerHTML.replace(/&gt;/g,"<font color=blue><b>></b></font>") ;
    
    el.innerHTML = el.innerHTML.replace(/(text)(\/)(javascript)/g,"<font color=pink>$1$2$3</font>") ;
    
    el.innerHTML = el.innerHTML.replace(/function/g,"<font color=blue><b>function</b></font>") ;
    
    el.innerHTML = el.innerHTML.replace(/\{/g,"<font color=deeppink><b>{</b></font>") ;
    el.innerHTML = el.innerHTML.replace(/\}/g,"<font color=deeppink><b>}</b></font>") ;
    
    el.innerHTML = el.innerHTML.replace(/var/g,"<font color=red>var</font>") ;
    el.innerHTML = el.innerHTML.replace(/el/g,"<font color=lime>el</font>") ;
    el.innerHTML = el.innerHTML.replace(/ ;/g,"<font color=blue><b> ;</b></font>") ;
    
    el.innerHTML = el.innerHTML.replace(/\(/g,"<font color=gold>(</font>") ;
    el.innerHTML = el.innerHTML.replace(/\)/g,"<font color=gold>)</font>") ;
    
    el.innerHTML = el.innerHTML.replace(/'/g,"<font color=lime><b>'</b></font>") ;
    
    el.innerHTML = el.innerHTML.replace(/\[/g,"<font color=skyblue><b>[</b></font>") ;
    el.innerHTML = el.innerHTML.replace(/\]/g,"<font color=skyblue><b>]</b></font>") ;
    
    el.innerHTML = el.innerHTML.replace(/for/g,"<font color=deeppink><b>for</b></font>") ;
    
    // el.innerHTML = el.innerHTML.replace(/(\d)/g,"<font color=red>$1</font>") ;
    el.innerHTML = el.innerHTML.replace(/getElementsByTagName/g,"<font color=turquoise><b>getElementsByTagName</b></font>") ;
    el.innerHTML = el.innerHTML.replace(/innerHTML/g,"<font color=lightseagreen><b>innerHTML</b></font>") ;
    
    }
    
    </script>
    <body>
    
    <pre>
    &lt;script type="text/javascript"&gt;
    
    window.onload = function () {
    var a = 5 ;
    var i ;
    var el = document.getElementsByTagName('body')[0];
    for(i = 0 ; i &lt; a ; i++)
    {
    el.innerHTML += i ;
    }
    }
    
    &lt;/script&gt;
    
    </pre>
    
    </body>
    When I added
    el.innerHTML = el.innerHTML.replace(/(\d)/g,"<font color=red>$1</font>") ;

    function is black.
    for is red.
    ' is black.
    { and } is red.

    on Opera
    Code:
      
    <script type="text/javascript">
    
    window.onload = function () {
    
    var el = document.getElementsByTagName('pre')[0];
    el.style.fontSize ="16px";
    // el.style.backgroundColor ="aqua";
    
    el.innerHTML = el.innerHTML.replace(/&lt;/g,"<font color=blue><b><</b></font>") ;
    el.innerHTML = el.innerHTML.replace(/&gt;/g,"<font color=blue><b>></b></font>") ;
    
    el.innerHTML = el.innerHTML.replace(/(text)(\/)(javascript)/g,"<font color=pink>$1$2$3</font>") ;
    
    el.innerHTML = el.innerHTML.replace(/function/g,"<font color=blue><b>function</b></font>") ;
    
    el.innerHTML = el.innerHTML.replace(/\{/g,"<font color=deeppink><b>{</b></font>") ;
    el.innerHTML = el.innerHTML.replace(/\}/g,"<font color=deeppink><b>}</b></font>") ;
    
    el.innerHTML = el.innerHTML.replace(/var/g,"<font color=red>var</font>") ;
    el.innerHTML = el.innerHTML.replace(/el/g,"<font color=lime>el</font>") ;
    el.innerHTML = el.innerHTML.replace(/ ;/g,"<font color=blue><b> ;</b></font>") ;
    
    el.innerHTML = el.innerHTML.replace(/\(/g,"<font color=gold>(</font>") ;
    el.innerHTML = el.innerHTML.replace(/\)/g,"<font color=gold>)</font>") ;
    
    el.innerHTML = el.innerHTML.replace(/'/g,"<font color=lime><b>'</b></font>") ;
    
    el.innerHTML = el.innerHTML.replace(/\[/g,"<font color=skyblue><b>[</b></font>") ;
    el.innerHTML = el.innerHTML.replace(/\]/g,"<font color=skyblue><b>]</b></font>") ;
    
    el.innerHTML = el.innerHTML.replace(/for/g,"<font color=deeppink><b>for</b></font>") ;
    
    el.innerHTML = el.innerHTML.replace(/(\d)/g,"<font color=red>$1</font>") ;
    el.innerHTML = el.innerHTML.replace(/getElementsByTagName/g,"<font color=turquoise><b>getElementsByTagName</b></font>") ;
    el.innerHTML = el.innerHTML.replace(/innerHTML/g,"<font color=lightseagreen><b>innerHTML</b></font>") ;
    
    }
    
    </script>
    <body>
    
    <pre>
    &lt;script type="text/javascript"&gt;
    
    window.onload = function () {
    var a = 5 ;
    var i ;
    var el = document.getElementsByTagName('body')[0];
    for(i = 0 ; i &lt; a ; i++)
    {
    el.innerHTML += i ;
    }
    }
    
    &lt;/script&gt;
    
    </pre>
    
    </body>
    " wil be "
    = wil be =
    ++ wil be ++
    += wil be +=
    How can I do? Thanks...

    I learned how it does:
    Quote Originally Posted by Fang
    Code:
    el.innerHTML = el.innerHTML.replace(/"/g,"<font color=lightgreen><b>\"</b></font>") ; // must be the first replace done!
    el.innerHTML = el.innerHTML.replace(/[^+=\w]=/g,"<font color=blue><b> =</b></font>") ; // exclude +, = and alphanumeric characters before =
    el.innerHTML = el.innerHTML.replace(/\+=/g,"<font color=gold><b>+=</b></font>") ; 
    el.innerHTML = el.innerHTML.replace(/\+\+/g,"<font color=blue><b>++</b></font>") ;
    Last edited by muazzez; Feb 11, 2007 at 08:20.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •