textRange的findText()方法详解
最近在做浏览器的的查找功能,关键字高亮和向上向下查找都差不多了
遇到了全字匹配和区分大小写的问题。
后来到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> | [建站心得]IIS如何绑定中文域名方法详解 [操作系统]用添加任务计划来实现电脑自动关机的方法详解 [学习帮助]快递运费计算方法详解 [电脑技术]在右键新建里添加新建word和excel方法详解 [办公软件]Excel 编号,Excel 自动编号,Excel 编号自动添加方… [MySql]批处理备份mysql数据库 [MySql]批处理命令BAT备份MySQL数据库 [MySql]用批处理自动备份MySQL数据库方法详解 [MySql]MySQL备份和恢复方法详解 [Web开发]解决“无法在发生错误时创建会话,请检查 PHP 或网…
|