Click here to Skip to main content
Click here to Skip to main content
Go to top

A generic text escape tool done in JavaScript

, 30 Nov 2012
Rate this:
Please Sign up or sign in to vote.
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.).

<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).

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

Strip New Lines (LF - Line Feed)

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

Strip Carriage Returns (CR)

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

Strip Form Feeds (FF)

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

Strip Horizontal Tabs (HT)

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

Strip Vertical Tabs (VT)

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

Strip Spaces

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

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

<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:

<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)

Share

About the Author

Alex Fotios
Software Developer Freelancer
Brazil Brazil
I have a keen interest in IT Security, Internet applications, and systems/embedded development. My recent research interests have included secure networks, models of trust, trusted agents, information exchange, and software development methodologies.
 
http//linkedin.com/in/fotios
https://www.elance.com/s/fotios/
Follow on   Twitter

Comments and Discussions

 
Questionno bad intentions :< PinmemberDharmindar1-Dec-12 23:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 30 Nov 2012
Article Copyright 2012 by Alex Fotios
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid