wolf
07-28-2002, 03:20 AM
I am trying to replace the strings in a textarea, using:
function replace(string,text,by)
{
var strLength = string.length, txtLength = text.length;
if ((strLength == 0) || (txtLength == 0)) return string;
var i = string.indexOf(text);
if ((!i) && (text != string.substring(0,txtLength))) return string;
if (i == -1) return string;
var newstr = string.substring(0,i) + by;
if (i+txtLength < strLength)
{
newstr += replace(string.substring(i+txtLength,strLength),text,by,1);
}
return newstr;
}
The problem is when I have to test for the string to be deleted before calling the replace function.
function makeText()
{
var str = "This is some text\n";
document.all['textarea1'].value = str;
}
function delText()
{
var str = "some text\n";
var newstr = document.all['textarea1'.value;
if (newstr.match(str))
{
replace(newstr,str,'');
document.all['textarea1'].value = newstr;
}
else
alert("error");
}
If str = "some text\n", the match is not found.
If str = "some text", the newline is left in the textarea.
I am using
newstr = replace(replace(newstr,result,'\n'),'\n\n','');
in another part of the program successfully.
Please help.
wolf
function replace(string,text,by)
{
var strLength = string.length, txtLength = text.length;
if ((strLength == 0) || (txtLength == 0)) return string;
var i = string.indexOf(text);
if ((!i) && (text != string.substring(0,txtLength))) return string;
if (i == -1) return string;
var newstr = string.substring(0,i) + by;
if (i+txtLength < strLength)
{
newstr += replace(string.substring(i+txtLength,strLength),text,by,1);
}
return newstr;
}
The problem is when I have to test for the string to be deleted before calling the replace function.
function makeText()
{
var str = "This is some text\n";
document.all['textarea1'].value = str;
}
function delText()
{
var str = "some text\n";
var newstr = document.all['textarea1'.value;
if (newstr.match(str))
{
replace(newstr,str,'');
document.all['textarea1'].value = newstr;
}
else
alert("error");
}
If str = "some text\n", the match is not found.
If str = "some text", the newline is left in the textarea.
I am using
newstr = replace(replace(newstr,result,'\n'),'\n\n','');
in another part of the program successfully.
Please help.
wolf