faqts : Computers : Programming : Languages : JavaScript : Document

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

10 of 19 people (53%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

how can I replace characters in a HTML- formated text without changing HTML ?

Oct 23rd, 2001 02:55
Andy Maurer,


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Untitled</TITLE>
<SCRIPT language="javascript">
/*
	This function replaces within a HTML- formated original string
	ALL occurences of a given string or character (stored in
	var whichstr) with ANY other given string or character (stored
	in var withstr) and leaves the HTML- tags and/or special 
characters
	like &amp; as they are. Of course unless this character has to 
be
	replaced.

****************************************************************

	THIS FUNCTION MAY BE USED AND COPIED without restrictions,
	but it would be nice if you mention my name ;-))

****************************************************************
	Copyright: 22.10.2001 by Andreas Maurer, Tubingen, Germany
****************************************************************
*/
function clreplace() {
	var original = document.editwin.DocumentHTML.value;
	var whichstr; // the string to be replaced
	var withstr; // the substitute
	var tagstart;// position for the beginning of a HTML- tag
	var notag; // string that definetely contains no tag
	var posspecial; // position for begin of a special character
	var nospecial; // string is definetely no specialcharacter
	var mightbespecial; // string might be a special character PLUS 
other strings
	var replacedspecial; // all special characters got replaced with
						 // their "real" 
characters
	var stringtoreplace; // the "cleaned up" string in which you 
need
						 // to look for and 
might have to replace the characters
	var firstfinalstr; //  the first string with replaced characters
	var mightbetag; // string might contain a tag PLUS 
other "normal" strings
	var tagend; // position for the end of a HTML- tag
	var tag; // the HTML- tag between tagstart and tagend
	var finalstr = new Array(); // contains the first string with 
replaced
								// 
characters plus the following HTML- tag
	var k = 0; // counter for finalstr
	var endstr; // the final string with all replaced characters
	var m = 0; // counter to prevent the script from running into a 
permanent loop

	whichstr = prompt("Geben Sie bitte die zu ersetzende 
Zeichenkette ein:","");
	if (whichstr != null && whichstr.length != 0) {
		withstr = prompt ("Geben Sie nun die einzufügende 
Zeichenkette ein:","");
		if (withstr != null) { // && withstr.length != 0) {
		while (original.length > 0 && m < original.length) {
			m++;
			tagstart = original.indexOf("<");
			if (tagstart == -1) {
/*
	if tagstart equals -1 (there is no html-tag in the string) you 
can save a lot of
	time and directly go to the replacement of the characters.
*/
				notag = original;
//				alert (notag);
			} else {
				notag = original.substring (0,tagstart);
			}
				posspecial = notag.indexOf("&");
				if (posspecial != -1) {
					nospecial = notag.substring
(0,posspecial);
					mightbespecial = notag.substring
(posspecial,notag.length);
/*  Replacing the specialcharacters with their equivalent "real"
	characters. This is needed to avoid problems with replacing
	e.g. the character "a" in a string that contains an ampersand,
	similar like: ab&amp;ca  (the ampersand is "translated" into 
&amp;
	If you don't replace the special characters the replaced string
	would look like Xb&Xmp;cX
*/
					replacedspecial = 
mightbespecial.replace(/&lt;/g,"<");
					replacedspecial = 
replacedspecial.replace(/&gt;/g,">");
					replacedspecial = 
replacedspecial.replace(/&nbsp;/g," ");
					replacedspecial = 
replacedspecial.replace(/&amp;/g,"&");
					stringtoreplace = nospecial + 
replacedspecial;
				} else {
					stringtoreplace = notag;
				}
				for (i = 0; i <= 
stringtoreplace.length; i++) {
					firstfinalstr = 
stringtoreplace.replace(whichstr, withstr);
					stringtoreplace = firstfinalstr;
				}
				firstfinalstr_temp = firstfinalstr;
/*
	Rechanging the "real" special characters into their HTML- 
equivalents
	and therefore restoring the original appearance.
*/
				firstfinalstr = 
firstfinalstr_temp.replace(/&/g,"&amp;");
				firstfinalstr = firstfinalstr.replace
(/</g,"&lt;");
				firstfinalstr = firstfinalstr.replace
(/>/g,"&gt;");
				for (j=0; j < firstfinalstr.length; 
j++) {
					firstfinalstr = 
firstfinalstr.replace(" ","&nbsp;");
				}
				if (tagstart != -1) {
					mightbetag = original.substring
(tagstart,original.length);
					tagend = mightbetag.indexOf
(">");
					tag = mightbetag.substring
(0,tagend+1);
					finalstr[k] = firstfinalstr + 
tag;
//					alert ("finalstr["+k+"]:: 
\n\n " + finalstr[k]);
					original = mightbetag.substring
(tag.length,original.length);
					k++;
				} else {
					finalstr[k] = firstfinalstr;
					original = firstfinalstr;
				}
			}
		}
		endstr = finalstr.join("");
		document.editwin.DocumentHTML.value = endstr;
	}
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#ffff00">
<FORM NAME="editwin">
		<TABLE CELLSPACING="2" CELLPADDING="2" BORDER="1">
				<TR>
						<TD>
							
	<TEXTAREA NAME="DocumentHTML" COLS="50" ROWS="10"></TEXTAREA>
						</TD>
				</TR>
				<TR>
						<TD>
								<INPUT 
TYPE="Button" NAME="" VALUE="Ersetzen Funktion aufrufen" ALIGN="MIDDLE" 
onClick="clreplace()">
						</TD>
				</TR>
		</TABLE>
</FORM>
</BODY>
</HTML>