呵呵,一直以来都是去别人BLOG搜刮资料
这次偶也写一点分享一下。
最近在做飞腾浏览器(FlyIe)的的查找功能,(打下广告@_@)
关键字高亮和向上向下查找都差不多了
遇到了全字匹配和区分大小写的问题。
后来到MSDN一查,原来findText()方法都有。
findText Method
Searches for text in the document and positions the start and end points of the range to encompass the search string.
Syntax
bFound = TextRange.findText(sText [, iSearchScope] [, iFlags])
Parameters
sText Required. String that specifies the text to find.
iSearchScope Optional. Integer that specifies the 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. Integer that specifies one or more of the following flags to indicate the type of search: 0 Default. Match partial words.
0 Default. Match partial words.
1 Match backwards.
2 Match whole words only.
4 Match case.
131072 Match bytes.
536870912 Match diacritical marks.
1073741824 Match Kashida character.
2147483648 Match AlefHamza character.
iFlags 参数为0表示部分匹配,比如
@a,@ADD,@a Abba @AB 超找@a 为四个
iFlags 参数为1表示查找最后一个关键字,比如
@a,@ADD,@a Abba @AB 查找@a 就到 @AB 这里去了
iFlags 参数为2表示全字匹配,比如
@a,@ADD,@a Abba @AB 查找@a 就到 只有一个就是 @a
iFlags 参数为3表示全字匹配,比如
@a,@ADD,@a Abba @AB 查找@a 就到 只有一个就是 @a
iFlags 参数为4表示大小写匹配,比如
@a,@ADD,@a Abba @AB 查找a 就到 只有三个
iFlags 参数为5表示按字节匹配
剩下几个参数不知道什么意思,也不重要就不说了。
那假如即要全字匹配又要大小写匹配怎么办呢?
很简单
If oRange.findText(nWord, , 2) Then
If oRange.findText(nWord, , 4) Then
nPosBM = oRange.getBookmark
'Call oRange.moveToBookmark(nPosBM)
Call oRange.Select
FindWord2 = True
End If
Else
'Set nPosBM = Nothing
nPosBM = vbNullString
FindWord2 = False
End If
加个IF 循环就好了
其他参数就靠其他大虾解释了
This example creates a TextRange over the body of the document, and then uses the findText method to search for text with various flag combinations. The results are indicated in the example code comments.
<HTML> <BODY> Leonardo da Vinci was one of the great masters of the High Renaissance, especially in painting, sculpture, architecture, engineering, and science. </BODY> </HTML> <SCRIPT> var oRange = document.body.createTextRange(); // record the current position in a bookmark var sBookMark = oRange.getBookmark(); // true - case-insensitive and partial word match oRange.findText('leo'); // reset the range using the bookmark oRange.moveToBookmark(sBookMark); // false - matches whole words only oRange.findText('engineer', 0, 2); oRange.moveToBookmark(sBookMark); // false - case-sensitive oRange.findText('high', 0, 4); oRange.moveToBookmark(sBookMark); // true - case-sensitive and matches whole words oRange.findText('Leonardo', 0, 6); // the degenerate case oRange.moveToBookmark(sBookMark); // make the range degenerate oRange.collapse(); // false - must specify large character count in this case oRange.findText('Leonardo', 0, 6); // true - no third parameter passed, so no count needed oRange.findText('Leonardo'); // true - a large count covers the range oRange.findText('Leonardo', 1000000000, 6); </SCRIPT> |