Two Search and Replace Approaches (with Undo)
/*
JavaScript Bible, Fourth Edition
by Danny Goodman
Publisher: John Wiley & Sons CopyRight 2001
ISBN: 0764533428
*/
<HTML>
<HEAD>
<TITLE>TextRange.findText() Method</TITLE>
<SCRIPT LANGUAGE="JavaScript">
// global range var for use with Undo
var rng
// return findText() third parameter arguments
function getArgs(form) {
var isCaseSensitive = (form.caseSensitive.checked) ? 4 : 0
var isWholeWord = (form.wholeWord.checked) ? 2 : 0
return isCaseSensitive ^ isWholeWord
}
// prompted search and replace
function sAndR(form) {
var srchString = form.searchString.value
var replString = form.replaceString.value
if (srchString) {
var args = getArgs(form)
rng = document.body.createTextRange()
rng.moveToElementText(rights)
clearUndoBuffer()
while (rng.findText(srchString, 10000, args)) {
rng.select()
rng.scrollIntoView()
if (confirm("Replace?")) {
rng.text = replString
pushUndoNew(rng, srchString, replString)
}
rng.collapse(false)
}
}
}
// unprompted search and replace with counter
function sAndRCount(form) {
var srchString = form.searchString.value
var replString = form.replaceString.value
var i
if (srchString) {
var args = getArgs(form)
rng = document.body.createTextRange()
rng.moveToElementText(rights)
for (i = 0; rng.findText(srchString, 10000, args); i++) {
rng.text = replString
pushUndoNew(rng, srchString, replString)
rng.collapse(false)
}
if (i > 1) {
clearUndoBuffer()
}
}
document.all.counter.innerText = i
}
// BEGIN UNDO BUFFER CODE
// buffer global variables
var newRanges = new Array()
var origSearchString
// store original search string and bookmarks of each replaced range
function pushUndoNew(rng, srchString, replString) {
origSearchString = srchString
rng.moveStart("character", -replString.length)
newRanges[newRanges.length] = rng.getBookmark()
}
// empty array and search string global
function clearUndoBuffer() {
document.all.counter.innerText = "0"
origSearchString = ""
newRanges.length = 0
}
// perform the undo
function undoReplace() {
if (newRanges.length && origSearchString) {
for (var i = 0; i < newRanges.length; i++) {
rng.moveToBookmark(newRanges[i])
rng.text = origSearchString
}
document.all.counter.innerText = i
clearUndoBuffer()
}
}
</SCRIPT>
</HEAD>
<BODY>
<H1>TextRange.findText() Method</H1>
<HR>
<FORM>
<P>Enter a string to search for in the following text:
<INPUT TYPE="text" NAME="searchString" SIZE=20 VALUE="Law">
<INPUT TYPE="checkbox" NAME="caseSensitive">Case-sensitive
<INPUT TYPE="checkbox" NAME="wholeWord">Whole words only</P>
<P>Enter a string with which to replace found text:
<INPUT TYPE="text" NAME="replaceString" SIZE=20 VALUE="legislation"></P>
<P><INPUT TYPE="button" VALUE="Search and Replace (with prompt)"
onClick="sAndR(this.form)"></P>
<P><INPUT TYPE="button" VALUE="Search, Replace, and Count (no prompt)"
onClick="sAndRCount(this.form)">
<SPAN ID="counter">0</SPAN> items found and replaced.</P>
<P><INPUT TYPE="button" VALUE="Undo Search and Replace"
onClick="undoReplace()"></P>
</FORM>
<DIV ID="rights">
<A NAME="article1">
<H2>ARTICLE I</H2>
</A>
<P>
Congress shall make no law respecting an establishment of religion,
or prohibiting the free exercise thereof; or abridging the freedom of speech,
or of the press; or the right of the people peaceably to assemble, and to
petition the government for a redress of grievances.
</P>
[The rest of the text is snipped for printing here, but it is on the CD-ROM
version.]
</DIV>
</BODY>
</HTML>
Related examples in the same category