Text Range Search and Replace (IE only) : String « Language Basics « JavaScript DHTML

JavaScript DHTML
1. Ajax Layer
2. Data Type
3. Date Time
4. Development
5. Document
6. Dojo toolkit
7. Event
8. Event onMethod
9. Ext JS
10. Form Control
11. GUI Components
12. HTML
13. Javascript Collections
14. Javascript Objects
15. Javascript Properties
16. jQuery
17. Language Basics
18. Mochkit
19. Mootools
20. Node Operation
21. Object Oriented
22. Page Components
23. Rico
24. Scriptaculous
25. Security
26. SmartClient
27. Style Layout
28. Table
29. Utilities
30. Window Browser
31. YUI Library
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
JavaScript DHTML » Language Basics » String 
Text Range Search and Replace (IE only)
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- 
     Example File From "JavaScript and DHTML Cookbook"
     Published by O'Reilly & Associates
     Copyright 2003 Danny Goodman
-->
<html>
<head>
<title>Recipe 15.3</title>
<style id="mainStyle" type="text/css">
html {background-color:#cccccc}
body {background-color:#eeeeee; font-family:Tahoma,Arial,Helvetica,sans-serif; font-size:12px;
    margin-left:15%; margin-right:15%; border:3px groove darkred; padding:15px}
h1 {text-align:right; font-size:1.5em; font-weight:bold}
h2 {text-align:left; font-size:1.1em; font-weight:bold; text-decoration:underline}
.buttons {margin-top:10px}
</style>

<script type="text/javascript">
// Global range object variable
var rng;

// Return TextRange.findText() third parameter arguments
function getArgs(caseSensitive, wholeWord) {
    var isCaseSensitive = (caseSensitive0;
    var isWholeWord = (wholeWord0;
    return isCaseSensitive ^ isWholeWord;
}

// Unprompted search and replace
function srBatch(container, search, replace, caseSensitive, wholeWord) {
    if (search) {
        var args = getArgs(caseSensitive, wholeWord);
        rng = document.body.createTextRange();
        rng.moveToElementText(container);
        clearUndoBuffer();
        for (var i = 0; rng.findText(search, 1000000, args); i++) {
            rng.text = replace;
            pushUndoNew(rng, search, replace);
            rng.collapse(false)  ;   
        }
    }
}

// Prompted search and replace
function srQuery(container, search, replace, caseSensitive, wholeWord) {
    if (search) {
        var args = getArgs(caseSensitive, wholeWord);
        rng = document.body.createTextRange();
        rng.moveToElementText(container);
        clearUndoBuffer();
        while (rng.findText(search, 10000, args)) {
            rng.select();
            rng.scrollIntoView();
            if (confirm("Replace?")) {
                rng.text = replace;
                pushUndoNew(rng, search, replace);
            }
            rng.collapse(false)  ;
        }    
    }
}

/****************
    UNDO BUFFER
*****************/
// Temporary storage of undo information
var undoObject = {origSearchString:"",newRanges :[]};

// Store original search string and bookmarks of each replaced range
function pushUndoNew(rng, srchString, replString) {
    undoObject.origSearchString = srchString;
    rng.moveStart("character", -replString.length);
    undoObject.newRanges[undoObject.newRanges.length= rng.getBookmark();
}

// Empty array and search string global
function clearUndoBuffer() {
    undoObject.origSearchString = "";
    undoObject.newRanges.length = 0;
}

// Perform the undo
function undoReplace() {
    if (undoObject.newRanges.length && undoObject.origSearchString) {
        for (var i = 0; i < undoObject.newRanges.length; i++) {
            rng.moveToBookmark(undoObject.newRanges[i]);
            rng.text = undoObject.origSearchString;
        }
        clearUndoBuffer();
    }
}


</script>
<script type="text/javascript">
// process "Search and Replace (with prompt)"
function process1(form, container) {
    var search = form.searchString.value
    var replace = form.replaceString.value
  var caseSensitive = form.caseSensitive.checked
  var wholeWord = form.wholeWord.checked;
  srQuery(container, search, replace, caseSensitive, wholeWord)
}
// process "Search, Replace (no prompt)"
function process2(form, container) {
    var search = form.searchString.value
    var replace = form.replaceString.value
  var caseSensitive = form.caseSensitive.checked
  var wholeWord = form.wholeWord.checked;
  srBatch(container, search, replace, caseSensitive, wholeWord)
}
</script>
</head>
<body>
<h1>IE TextRange Search and Replace</h1>
<hr /> 

<form>
<p>Enter a string to search for in the following text:
<input type="text" name="searchString" size="20" value="Law"> &nbsp;
<input type="checkbox" name="caseSensitive">Case-sensitive &nbsp;

<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="process1(this.form, document.body)"></p>
<p><input type="button" value="Search and Replace No Prompt" onClick="process2(this.form, document.body)"></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>

<a name="article2">
<h2>ARTICLE II</h2>
</a>
<p>
A well regulated militia, being necessary to the security of a free state, the right of the people to keep and bear arms, shall not be infringed.
</p>

<a name="article3">
<h2>ARTICLE III</h2>
</a>

<p>
No soldier shall, in time of peace, be quartered in any house, without the consent of the owner, nor in time of war, but in in a manner to be prescribed by law.
</p>

<a name="article4">
<h2>ARTICLE IV</h2>
</a>
<p>
The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.
</p>

<a name="article5">
<h2>ARTICLE V</h2>
</a>
<p>

No person shall be held to answer for a capital, or otherwise infamous crime, unless on a presentment or indictment of a grand jury, except in cases arising in the land or naval forces, or in the militia, when in actual service in time of war or public danger; nor shall any person be subject for the same offense to be twice put in jeopardy of life or limb; nor shall be compelled in any criminal case to be a witness against himself, nor be deprived of life, liberty, or property, without due process of law; nor shall private property be taken for public use, without just compensation.
</p>

<a name="article6">
<h2>ARTICLE VI</h2>
</a>
<p>
In all criminal prosecutions the accused shall enjoy the right to a speedy and public trial, by an impartial jury of the state and district wherein the crime shall have been committed, which district shall have been previously ascertained by law, and to be informed of the nature and cause of the accusation; to be confronted with the witnesses against him; to have compulsory process for obtaining witnesses in his favor, and to have the assistance of counsel for his defense.
</p>

<a name="article7">
<h2>ARTICLE VII</h2>
</a>
<p>
In suits at common law, where the value in controversy shall exceed twenty dollars, the right of trial by jury shall be preserved, and no fact tried by a jury shall be otherwise re-examined in any court of the United States, than according to the rules of the common law.

</p>

<a name="article8">
<h2>ARTICLE VIII</h2>
</a>
<p>
Excessive bail shall not be required, nor excessive fines imposed, nor cruel and unusual punishments inflicted.
</p>

<a name="article9">
<h2>ARTICLE IX</h2>
</a>
<p>
The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
</p>

<a name="article10">
<h2>ARTICLE X</h2>
</a>
<p>
The powers not delegated to the United States by the Constitution, nor prohibited by it to the states, are reserved to the states respectively, or the the people.
</p>
</div>

</body>
</html>


           
         
    
  
Related examples in the same category
1. Demo all String methods
2. String utility: word count, replace and capitalize
3. Strip Commas
4. Counting the Words in a Text String
5. Reversing a String
6. Trimming a String Using Regular Expressions
7. String encode and decode
8. Capitalizing the First Letter in Each Word of a String
9. Playing with Strings
10.  Using the String Object's Link Method
11. Using a for Loop to Reverse a String
12.  Concatenate JavaScript String
13. String length: number of characters in a string.
14. String fontcolor(): a string in a specified color
15. String indexOf(): string position
16. String Validation
17. Using Quotes within Strings
18. Using the String Object
19. String toUpperCase
20. Lab for string.replace() and string.search()
21. Slicing a String
22. A String Object Prototype
23. Creating a Custom toString() Method
24. Reading a Portion of a String
25. Source Code for a Sample Page That Formats a String Object with the 'a' Tag
26. Source Code for Our String-Formatting Script
27. Adding a replace() Method to the String Object
28. Creating a Function That Will Search and Replace in Strings
29.  Using the indexOf() Method to Find All Occurrences of the Letter e in a Sentence
30. Methods and Properties of the String Object
31. String match(): returns the text if found
32. String substr() and substring(): returns a specified part of a string
33. String toLowerCase() and toUpperCase(): converts a string to lowercase and uppercase
34. Converting Strings to Upper Case
35. String encoder
36. Concatenate two string variables together
37. Search string value in an array
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.