Richard said:
I need a javascript function to hex-encode a plus sign so I can
pass the plus sign as an argument in a GET request. escape() and
encodeURI() don't do it (and probably shouldn't, because '+' is
a valid character in a URI). I could write a regexp, but the
environment I'm in won't allow replace(). I could write something
cumbersome using indexOf, but if there's something easier, like
a builtin function that encodes every character in the string, I'd
prefer that.
There is not.
I'm a newbie at js, so type slowly, please.
How many characters per minute are allowed in this newsgroup?
I cna tpye 500 wrods pre mnitue!!1 ;-)
Seriously, I'm afraid there is no easy solution, especially because the
available methods of your implementation are virtually unknown. Maybe
you are looking for this:
/**
* @author
* (C) 2001-2004 Thomas Lahn <
[email protected]>
* Distributed under the GNU GPL v2.
* @partof
*
http://pointedears.de/scripts/string.js
* @argument _ sText
* @argument string sReplaced
* @argument string sReplacement
* @return type string
*/
function replaceText(sText, sReplaced, sReplacement)
{
var result = "";
var t;
if (!sText && sText.constructor == String)
{
sText = this;
}
var sNewText = sText;
var a;
// alert(sText);
if (sText && sReplaced && sReplacement)
{
if (sText.replace)
{
sReplaced = sReplaced.replace(/\\/g, "\\\\");
/* Version 1.23.2002.4 bugfix: allows to replace \ with other
* strings, required for proper rxReplaced;
* Example (no quotes, no escaping):
* sReplaced (provided) "\\"
* sReplaced (evaluated) \
* sReplaced (replaced as formulated above) "\\\\"
* sReplaced (esc. in RegExp constructor) "\\\\"
* sReplaced (ev. in RegExp constructor) \\
* rxReplaced (with RegExp escaping) /\\/g
* rxReplaced (evaluated) all occurr. of \
*/
var rxReplaced = new RegExp(sReplaced, "g");
sText = sText.replace(rxReplaced, sReplacement);
result = sText;
}
else if (sText.split
&& (sReplaced = sReplaced.replace(/\\/g, "\\\\"))
&& (a = sText.split(sReplaced))
&& a.join)
{
result = a.join(sReplacement);
}
else
{
var i = sText.indexOf(sReplaced);
if (i > -1)
{
sNewText = sText.substring(0, i);
sNewText += sReplacement
+ replaceText(
sText.substring(i + sReplaced.length),
sReplaced,
sReplacement);
}
result = sNewText;
}
}
return result;
}
var f = this.encodeURI || this.escape || function dummy(s) { return s; };
var plusEscape = "%" + "+".charCodeAt(0).toString(16).toUpperCase();
var x = replaceText(f("foo"), "+", plusEscape);
This will use the return value of encodeURI() or escape(), if available,
to escape the usual characters (why reinvent the wheel?) which is passed
to replaceText() which uses two alternatives if String.prototype.replace()
is unavailable:
A) sText.split(sReplaced).join(sReplacement)
B) calling the method recursively until every occurrence was replaced
You wrote that String.prototype.replace() is not available. This method was
introduced in JavaScript 1.2, so your implementation may be of the level of
JavaScript 1.1. String.prototype.split() and Array.prototype.join() are
available there, so it is likely that your implementation will use
alternative A.
HTH
PointedEars