DHTML Methods
Searches for text in the document and positions the start and end points of the range to encompass the search string.
bFound = TextRange.findText (
sText [ , iSearchScope ] [ , iFlags ] )
sText |
Required. Text to find. |
iSearchScope |
Optional. Number of characters to search from the starting point of the range. A positive integer indicates a forward search; a negative integer indicates a backward search. |
iFlags |
Optional. One or more of the following flags indicating the type of search:
2 |
Match whole words only. |
4 |
Match case. |
|
Boolean. Returns TRUE if the search text is found, or FALSE otherwise.
A range has two distinct states: degenerate and nondegenerate. Analogous to a text editor, a degenerate range is like a text editor caret ( insertion point ); it does not actually select any characters. Instead it specifies a point between two characters. A degenerate range's end points are effectively next to each other. On the other hand, a nondegenerate range is like a text editor selection. A certain amount of text is selected, and the end points of the range are not next to each other.
The value passed for the iSearchScope parameter controls the part of the document, relative to the range, that is searched. The behavior of the findText method is dependent upon the state ( degenerate or nondegenerate ).
- If the range is degenerate, passing a large positive number will cause the text to the right of the range to be searched. Passing a large negative number causes the text to the left of the range to be searched.
- If the range is nondegenerate, passing 0 will cause only the text "selected" by the range to be searched.
- For a nondegenerate range passing a large positive number will cause the text to the right of the start of the range to be searched.
- For a nondegenerate range passing a large negative number will cause the text to the left of the end of the range to be searched.
This feature might not be available on non-Win32® platforms. See the Microsoft® Knowledge Base for the latest information on Internet Explorer® cross-platform compatibility.
The following example creates a TextRange over the body of the document and searches for text with various flag combinations. The results are indicated in the code comments.
Sample Code
<script language="JavaScript">
var oRange = document.body.createTextRange ( );
var sBookMark = oRange.getBookmark ( );
// record the current position in a bookmark
oRange.findText ( 'leo' );
// true - case-insensitive and partial word match
oRange.moveToBookmark ( sBookMark );
// reset the range using the bookmark
oRange.findText ( 'engineer', 0, 2 );
// false - matches whole words only
oRange.moveToBookmark ( sBookMark );
oRange.findText ( 'high', 0, 4 );
// false - case-sensitive
oRange.moveToBookmark ( sBookMark );
oRange.findText ( 'Leonardo', 0, 6 );
// true - case-sensitive and matches whole words
// the degenerate case
oRange.moveToBookmark ( sBookMark );
oRange.collapse ( );
// make the range degenerate
oRange.findText ( 'Leonardo', 0, 6 );
// false - must specify large character
// count in this case
oRange.findText ( 'Leonardo' );
// true - no third parameter passed,
// so no count needed
oRange.findText ( 'Leonardo', 1000000000, 6 );
// true - a large count covers the range
</script>
<body>
Leonardo da Vinci was one of the great masters of
the High Renaissance, especially in painting,
sculpture, architecture, engineering, and science.
</body>
TextRange