View Full Version : string replace problem?

07-28-2002, 04: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))
document.all['textarea1'].value = newstr;

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.


07-28-2002, 07:02 AM
i don't know if you have some reason to build your own function for this, but there's a method of strings for replacing text.

string.replace(original, replacement);

the only thing that i noticed in your functions was that in the following line, you have an opening bracket, but no closing one:
var newstr = document.all['textarea1'.value;

i suspect that this probably has something to do with the match() method. i'm in the middle of packing, so i can't check now, but tomorrow, i'll dig out my copy of the Guide ( see my signature ), and see if there's maybe something there that explains why you're having trouble.

07-30-2002, 03:39 AM

07-30-2002, 04:13 AM
well, as best i can make out, your problem does in fact come from using match. match is meant to be used with regular expressions, and you're using a string, which shouldn't mean anything, but apparently it does. try just checking for the existence of the string using indexOf. or, like i suggested before, just use the built in method.

07-30-2002, 05:16 AM
Been fiddling with this String method for a bit; you're welcome to it if it serves your needs. Can't quite figure out the above.

<script type="text/javascript" language="JavaScript">

String.prototype.replaceChars = function(old_str, new_str) {
var badChars = [/\//g , /\\/g , /\|/g , /\./g , /\(/g , /\)/g , /\$/g , /\[/g , /\^/g , /\*/g , /\\'/g , /\"/g , /\+/g , /\?/g];
var goodChars = ['\/' , '\\\\\' , '\\|' , '\\.' , '\\(' , '\\)' , '\\$' , '\\[' , '\\^' , '\\*' , '\\\\'' , '\\"' , '\\+' , '\\?'];
for (var i=0; i<badChars.length; ++i) old_str = old_str.replace(badChars[i], goodChars[i]);
var RE = new RegExp(old_str, 'g');
return this.replace(RE, new_str);

<textarea name="t1" rows="20" cols="80" wrap="virtual">
The whales had to be pushed off in bunches, he said. Individuals pushed off are likely to try to rejoin the group on the beach. The carcasses of the nine dead whales were being removed from the beach one by one. Technically pilot whales are big dolphins. They range from 12 feet to 25 feet long and from 1.5 tons to 3 tons or more for adults. They are also known as blackfish. Strandings of pilot whales are not unusual on Cape Cod and at a few other locations around the globe. Scientists are still speculating on the reasons for such strandings. While there is no firm conclusion, it is generally believed the whales play follow the leader when the lead whale in a pod becomes disoriented due to illness or gets lost in the shallow water.
<input type="button" value="Replace 'whales' with 'idiots'"
onclick="t1.value=t1.value.replaceChars('whales', 'idiots')"><br><br>
<input type="button" value="Replace 'idiots' with 'sex-crazed penguins'"
onclick="t1.value=t1.value.replaceChars('idiots', 'sex-crazed penguins')"><br><br>
<input type="button" value="Replace '. ' with '\n'"
onclick="t1.value=t1.value.replaceChars('. ', '.\n')">

07-30-2002, 06:35 AM
thanks for the help joh6nn and adios.