加载项经常需要基于文档文本运行。 每个内容控件都会公开搜索方法, (这包括 Body、Paragraph、Range、Table、TableRow 和基 ContentControl 对象) 。 此方法采用字符串 (或通配符表达式,) 表示要搜索的文本和 SearchOptions 对象。 它返回与搜索文本匹配的区域集合。
重要
Word客户端可能会限制可用的搜索选项。 有关当前支持的更多详细信息,请参阅 查找和替换文本。
搜索选项
搜索选项为多个用于定义搜索参数处理方式的布尔值集合。
| 属性 | 说明 |
|---|---|
ignorePunct |
获取或设置一个值,该值指示是否忽略单词之间的标点符号的值。 对应于“ 查找和替换 ”对话框中的“忽略标点字符”复选框。 |
ignoreSpace |
获取或设置一个值,该值指示是否忽略单词之间的所有空格。 对应于“ 查找和替换 ”对话框中的“忽略空白字符”复选框。 |
matchCase |
获取或设置一个值,该值指示是否执行区分大小写的搜索。 对应于“ 查找和替换 ”对话框中的“匹配大小写”复选框。 |
matchPrefix |
获取或设置一个值,该值指示是否匹配以搜索字符串开头的单词。 对应于“ 查找和替换 ”对话框中的“匹配前缀”复选框。 |
matchSuffix |
获取或设置一个值,该值指示是否匹配以搜索字符串结尾的单词。 对应于“ 查找和替换 ”对话框中的“匹配后缀”复选框。 |
matchWholeWord |
获取或设置一个值,该值用于指示是否查找操作仅限整个单词,而非较长单词的一部分的文字。 对应于“ 查找和替换 ”对话框中的“仅查找整个单词”复选框。 |
matchWildcards |
获取或设置一个值,该值指示搜索是否使用特殊搜索操作符执行。 对应于“ 查找和替换 ”对话框中的“使用通配符”复选框。 |
搜索特殊字符
下表列出了某些特殊字符的搜索表示法。
| 找到 | 表示法 |
|---|---|
| 段落标记 | ^p |
| 制表符 | ^t |
| 任何字符 | ^? |
| 任意数字 | ^# |
| 任何字母 | ^$ |
| 插入符号字符 | ^^ |
| 节字符 | ^% |
| 段落字符 | ^v |
| 分栏符 | ^n |
| Em 短划线 | ^+ |
| En 短划线 | ^= |
| 尾注标记 | ^e |
| 字段 | ^d |
| 脚注标记 | ^f |
| 图形 | ^g |
| 手动换行 | ^l |
| 手动分页符 | ^m |
| 非中断连字符 | ^~ |
| 不间断空格 | ^s |
| 可选连字符 | ^- |
| 分节符 | ^b |
| 空格 | ^w |
通配符指导
下表提供了与 Word JavaScript API 的搜索通配符相关的指导。
| 找到 | 通配符 | 示例 |
|---|---|---|
| 任意单个字符 | ? |
s?t 查找 sat 和 set。 |
| 任何字符的字符串 | * |
s*d 查找 sad 和 started。 |
| 单词的开头 | < |
<(inter) 查找 interesting 和 intercept,但不查找 splintered。 |
| 单词结尾 | > |
(in)> 查找 in 和 within,但不查找 interesting。 |
| 一个指定的字符 | [ ] |
w[io]n 查找 win 和 won。 |
| 此区域中的任何单个字符 | [-] |
[r-t]ight 查找 right、 sight和 tight。 区域必须按升序排列。 |
| 除括号中区域内的字符以外的任何单个字符 | [!x-z] |
t[!a-m]ck 查找 tock 和 tuck,但不查找 tack 或 tick。 |
| 恰好上一个字符或表达式的 n 个匹配项 | {n} |
fe{2}d 查找 feed 但未 fed找到 。 |
| 至少 出现上 一个字符或表达式的 n 次 | {n,} |
fe{1,}d 查找 fed 和 feed。 |
| 上一个字符或表达式的出现次数从 n 到 m | {n,m} |
10{1,3} 查找 10、 100和 1000。 |
| 前一个字符或表达式出现一次或多次 | @ |
lo@t 查找 lot 和 loot。 |
转义特殊字符
通配符搜索实质上与在正则表达式上搜索相同。 正则表达式中存在特殊字符,包括“[”、“]”、“ (”、“) ”、“{”、“}”、“*”、“?”、“<”、“”、“”、“>!”、“@”。 如果其中一个字符是代码要搜索的文本字符串的一部分,则需要对其进行转义,以便Word知道应该按字面量处理它,而不是正则表达式逻辑的一部分。 若要转义Word UI 搜索中的字符,请在该字符前面加上反斜杠字符 ('\') ,但若要以编程方式转义字符,请将其置于“[]”字符之间。 例如,“[*]*”搜索以“*”开头、后跟任意数量的其他字符的任何字符串。
示例
下面示例演示常见情况。
忽略标点符号搜索
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
// Queue a command to search the document and ignore punctuation.
const searchResults = context.document.body.search('video you', {ignorePunct: true});
// Queue a command to load the font property values.
searchResults.load('font');
// Synchronize the document state.
await context.sync();
console.log('Found count: ' + searchResults.items.length);
// Queue a set of commands to change the font for each found item.
for (let i = 0; i < searchResults.items.length; i++) {
searchResults.items[i].font.color = 'purple';
searchResults.items[i].font.highlightColor = '#FFFF00'; //Yellow
searchResults.items[i].font.bold = true;
}
// Synchronize the document state.
await context.sync();
});
基于前缀搜索
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
// Queue a command to search the document based on a prefix.
const searchResults = context.document.body.search('vid', {matchPrefix: true});
// Queue a command to load the font property values.
searchResults.load('font');
// Synchronize the document state.
await context.sync();
console.log('Found count: ' + searchResults.items.length);
// Queue a set of commands to change the font for each found item.
for (let i = 0; i < searchResults.items.length; i++) {
searchResults.items[i].font.color = 'purple';
searchResults.items[i].font.highlightColor = '#FFFF00'; //Yellow
searchResults.items[i].font.bold = true;
}
// Synchronize the document state.
await context.sync();
});
基于后缀搜索
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
// Queue a command to search the document for any string of characters after 'ly'.
const searchResults = context.document.body.search('ly', {matchSuffix: true});
// Queue a command to load the font property values.
searchResults.load('font');
// Synchronize the document state.
await context.sync();
console.log('Found count: ' + searchResults.items.length);
// Queue a set of commands to change the font for each found item.
for (let i = 0; i < searchResults.items.length; i++) {
searchResults.items[i].font.color = 'orange';
searchResults.items[i].font.highlightColor = 'black';
searchResults.items[i].font.bold = true;
}
// Synchronize the document state.
await context.sync();
});
使用通配符搜索
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
// Queue a command to search the document with a wildcard
// for any string of characters that starts with 'to' and ends with 'n'.
const searchResults = context.document.body.search('to*n', {matchWildcards: true});
// Queue a command to load the font property values.
searchResults.load('font');
// Synchronize the document state.
await context.sync();
console.log('Found count: ' + searchResults.items.length);
// Queue a set of commands to change the font for each found item.
for (let i = 0; i < searchResults.items.length; i++) {
searchResults.items[i].font.color = 'purple';
searchResults.items[i].font.highlightColor = 'pink';
searchResults.items[i].font.bold = true;
}
// Synchronize the document state.
await context.sync();
});
搜索特殊字符
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
// Queue a command to search the document for tabs.
const searchResults = context.document.body.search('^t');
// Queue a command to load the font property values.
searchResults.load('font');
// Synchronize the document state.
await context.sync();
console.log('Found count: ' + searchResults.items.length);
// Queue a set of commands to change the font for each found item.
for (let i = 0; i < searchResults.items.length; i++) {
searchResults.items[i].font.color = 'purple';
searchResults.items[i].font.highlightColor = 'pink';
searchResults.items[i].font.bold = true;
}
// Synchronize the document state.
await context.sync();
});
使用通配符搜索转义的特殊字符
如前面 转义特殊字符中所述,正则表达式使用特殊字符。 为了使通配符搜索以编程方式查找其中一个特殊字符,需要使用“[”和“]”对其进行转义。 此示例演示如何使用通配符搜索查找“{”特殊字符。
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
// Queue a command to search the document with a wildcard for an escaped opening curly brace.
const searchResults = context.document.body.search('[{]', { matchWildcards: true });
// Queue a command to load the font property values.
searchResults.load('font');
// Synchronize the document state.
await context.sync();
console.log('Found count: ' + searchResults.items.length);
// Queue a set of commands to change the font for each found item.
for (let i = 0; i < searchResults.items.length; i++) {
searchResults.items[i].font.color = 'purple';
searchResults.items[i].font.highlightColor = 'pink';
searchResults.items[i].font.bold = true;
}
// Synchronize the document state.
await context.sync();
});
尝试Script Lab中的代码示例
获取Script Lab加载项并试用本文中提供的代码示例。 若要了解详细信息,请参阅使用 Script Lab 探索 Office JavaScript API。
另请参阅
有关详细信息,请参阅以下内容:
- Word JavaScript 参考 API
- Script Lab中提供了相关Word代码示例:
- 查找和替换Word中的文本