Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

A Generic Text Escape Tool Done in JavaScript

0.00/5 (No votes)
30 Nov 2012CPOL 10.4K  
A generic text escape tool done in JavaScript

I needed to be able to escape long HTML and JS code chunks (sometimes mixed) for inclusion in perl style regex text substitution expressions. Doing this manually for a bit more than a few lines of code can quickly turn into a nightmare and I could not find another customizable text escaping tool online fast enough, so I wrote it myself since, as you can see, coding it is not involved at all. 

Below is the HTML and JavaScript code needed to implement it - a live version of the tool can be seen here: http://fotios.org/node/2687.

Enter the chars to be escaped below (with or without spaces between them, but don't use commas, colons, dots, etc.).

HTML
<input id="charsToEsc" type="text" />

Enter a single char to be used as the escape char here (the escape char itself will always be escaped itself if encountered - default escape char is backslash).

HTML
<input id="escChar" type="text" size="1" maxlength="1" value="\"/>

Strip New Lines (LF - Line Feed):

HTML
<input id="stripLF" type="checkbox" checked="checked" />

Strip Carriage Returns (CR):

HTML
<input id="stripCR" type="checkbox" checked="checked" />

Strip Form Feeds (FF):

HTML
<input id="stripFF" type="checkbox" checked="checked" />

Strip Horizontal Tabs (HT):

HTML
<input id="stripHT" type="checkbox" checked="checked" />

Strip Vertical Tabs (VT):

HTML
<input id="stripVT" type="checkbox" checked="checked" />

Strip Spaces:

HTML
<input id="stripSpace" type="checkbox" /> 

Type or copy-paste your text below and press one of the buttons:

HTML
<textarea id="textIn" rows="10" wrap="virtual" 
  style="width:100%"></textarea> <button  
  önclick="gteEscape();">Escape</button>  <button  
  önclick="gteEscapeAll();">Escape All</button>
  <textarea id="textOut" rows="10" wrap="virtual" 
  style="width:100%" readonly="readonly"></textarea>

Code is right here:

JavaScript
<script type="text/javascript">
function gteEscape()
{
    var charsToEsc = document.getElementById("charsToEsc").value;
    var escChar    = document.getElementById("escChar").value;
    
    var stripLF    = document.getElementById("stripLF").checked;
    var stripCR    = document.getElementById("stripCR").checked;
    var stripFF    = document.getElementById("stripFF").checked;
    var stripHT    = document.getElementById("stripHT").checked;
    var stripVT    = document.getElementById("stripVT").checked;
    
    var stripSpace = document.getElementById("stripSpace").checked;
    
    //alert(document.getElementById("stripLF").checked);
        
    var textIn = document.getElementById("textIn").value;
    var textOut = "";
    
    //alert("textIn is: " + textIn);
    
    if ( !(charsToEsc.length && escChar.length && textIn.length) )
    {
        alert("None of the text fields can be empty");
        return;
    }
    
    var len = textIn.length;
    
    for (var i = 0; i < len; i++)
    {
        //var ch = textIn.charAt(i);
        var ch = textIn[i];
        
        //alert("ch is: " + ch);
        //alert("textIn[i].charCodeAt(0) is: " + textIn[i].charCodeAt(0));
        //alert (ch == "\n");
        
        switch (ch)
        {
            case "\n": if (!stripLF) textOut += "\n"; break;
            case "\r": if (!stripCR) textOut += "\r"; break;
            case "\f": if (!stripFF) textOut += "\f"; break;
            case "\t": if (!stripHT) textOut += "\t"; break;
            case "\v": if (!stripVT) textOut += "\v"; break;
            
            case " ":  if (!stripSpace) textOut += " "; break;
            
            //case "\x0A": if (!stripLF) textOut += "\n"; break;
            //case "\x0D": if (!stripLF) textOut += "\n"; break;
            case escChar: textOut += escChar + escChar; break;
            
            default: 
                //alert("ch is: " + ch + " and index is: " + charsToEsc.indexOf(ch));
                if (charsToEsc.indexOf(ch) > -1) 
                    textOut += escChar + ch;
                else
                    textOut += ch;
        }
    }
    
    document.getElementById("textOut").value = textOut;
}

function gteEscapeAll()
{
    var charsToEsc = document.getElementById("charsToEsc").value;
    var escChar    = document.getElementById("escChar").value;
    
    var stripLF    = document.getElementById("stripLF").checked;
    var stripCR    = document.getElementById("stripCR").checked;
    var stripFF    = document.getElementById("stripFF").checked;
    var stripHT    = document.getElementById("stripHT").checked;
    var stripVT    = document.getElementById("stripVT").checked;
    
    var stripSpace = document.getElementById("stripSpace").checked;
        
    var textIn = document.getElementById("textIn").value;
    var textOut = "";
    
    
    if ( !(escChar.length && textIn.length) )
    {
        alert("Please provide an escape char AND the text to escape");
        return;
    }
    
    
    var len = textIn.length;    
    
    for (var i = 0; i < len; i++)
    {
        var ch = textIn[i];
        
        if (ch == "\n" || ch == "\r" || ch == "\f" || 
            ch == "\t" || ch == "\v" || ch == " ")
        {
            if (ch == "\n" && !stripLF)    textOut += ch; else
            if (ch == "\r" && !stripCR)    textOut += ch; else
            if (ch == "\f" && !stripFF)    textOut += ch; else
            if (ch == "\t" && !stripHT)    textOut += ch; else
            if (ch == "\v" && !stripVT)    textOut += ch; else
            if (ch == " "  && !stripSpace) textOut += ch;
        }
        else
        {
            textOut += escChar + ch;
        }
    }
    
    document.getElementById("textOut").value = textOut;        
}
</script> 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)