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 char
s 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;
var textIn = document.getElementById("textIn").value;
var textOut = "";
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[i];
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 escChar: textOut += escChar + escChar; break;
default:
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>