Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string parsing problems

Status
Not open for further replies.

malonep

Programmer
Jul 14, 2004
44
CA
Hi,

I have this small function to replace characters so symbols can be properly displayed in a textarea. In one function it is called multiple times
Code:
  comment = replaceAll(comment,'#','#');
	    comment = replaceAll(comment,'>','>');
	    comment = replaceAll(comment,'&lt;','<');
   	    comment = replaceAll(comment,'&#58;','?');
	    comment = replaceAll(comment,'&#39;','\''); 
	    comment = replaceAll(comment,'&#37;','%');
	    comment = replaceAll(comment,'&#45;','-');
	    comment = replaceAll(comment,'&#47;','//');
	    comment = replaceAll(comment,'&#34;','"');
The function that does this is
Code:
function replaceAll(str,old,to) {
        var index;
        var newstr;
   	 	
        newstr = str;
        index = newstr.indexOf( old );	
   	while ( index > -1 ) {
   		newstr = newstr.replace( old, to ); 
   		index = newstr.indexOf( old );
   	}
   	return newstr;
}
This method is working perfectly for windows ie/netscape and mac safari but for some reason on a mac IE I get the error "Invalid Character" and the line number indicates "var index;" line in the replace all function

Any help would be appreciated
Thanks,
malonep
 
Hi,

I changed index to be idx and the same issue is occurring

Thanks,
 
Why are you using a replaceAll function when you can use regex?
Code:
  comment = comment.replace(/#/g,'&#35;');
  comment = comment.replace(/>/g,'&gt;');
  comment = comment.replace(/</g,'&lt;');
  comment = comment.replace(/\?/g,'&#58;');
  comment = comment.replace(/'/g,'&#39;');
  comment = comment.replace(/%/g,'&#37;');
  comment = comment.replace(/-/g,'&#45;');
  comment = comment.replace(/\//g,'&#47;');
  comment = comment.replace(/"/g,'&#34;');
Also, you are calling
Code:
   comment = replaceAll(comment,'&#47;','//');
"//" initalizes a comment. Try using "\/".

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top