Share via


Word アドインの検索オプションを使用してテキストを検索する

アドインは、ドキュメントのテキストに基づいて動作することが必要な場合がよくあります。 検索メソッドは、すべてのコンテンツ コントロールによって公開されます (これには 、BodyParagraphRangeTableTableRow、および基本 ContentControl オブジェクトが含まれます)。 このメソッドは、検索するテキストと SearchOptions オブジェクトを表す文字列 (またはワイルドカード式) を受け取ります。 これにより、検索テキストと一致する範囲のコレクションが返されます。

重要

Word クライアントでは、使用可能な検索オプションが制限される場合があります。 現在のサポートの詳細については、「 テキストの検索と置換」を参照してください。

検索オプション

検索オプションとは、検索パラメーターをどのように処理するかを定義するブール値のコレクションです。

プロパティ 説明
ignorePunct 単語間の句読点文字をすべて無視するかどうかを示す値を取得するか設定します。 [ 検索と置換 ] ダイアログ ボックスの [句読点を無視する] チェック ボックスに対応します。
ignoreSpace 単語間のすべての空白を無視するかどうかを示す値を取得または設定します。 [ 検索と置換 ] ダイアログ ボックスの [空白文字を無視する] チェック ボックスに対応します。
matchCase 大文字と小文字を区別する検索を実行するかどうかを示す値を取得または設定します。 [ 検索と置換 ] ダイアログ ボックスの [大文字と小文字の一致] チェック ボックスに対応します。
matchPrefix 検索文字列で始まる単語と一致するかどうかを示す値を取得または設定します。 [ 検索と置換 ] ダイアログ ボックスの [プレフィックスの一致] チェック ボックスに対応します。
matchSuffix 検索文字列で終わる単語と一致するかどうかを示す値を取得または設定します。 [ 検索と置換 ] ダイアログ ボックスの [サフィックスの一致] チェック ボックスに対応します。
matchWholeWord 長い単語の一部のテキストではなく、単語全体のみを検索するかどうかを示す値を取得するか設定します。 [検索 と置換 ] ダイアログ ボックスの [単語全体のみを検索する] チェック ボックスに対応します。
matchWildcards 特殊な検索演算子を使用して検索を実行するかどうかを示す値を取得または設定します。 [ 検索と置換 ] ダイアログ ボックスの [ワイルドカードを使用する] チェック ボックスに対応します。

特殊文字のSearch

次の表に、特定の特殊文字の検索表記を示します。

見つけます 表記
段落記号 ^P
タブ マーク ^T
任意の文字 ^?
任意の桁 ^#
任意の文字 ^$
キャレット文字 ^^
セクション文字 ^%
段落文字 ^V
列区切り ^N
Em ダッシュ ^+
En dash ^=
文末脚注マーク ^E
フィールド ^D
脚注マーク ^F
グラフィック ^G
手動改行 ^L
手動改ページ ^M
非改行ハイフン ^~
非改行スペース ^S
オプションのハイフン ^-
セクション区切り ^B
空白 ^W

ワイルドカードに関する説明

次の表では、Word JavaScript API の検索ワイルドカードに関するガイダンスを示します。

見つけます ワイルドカード サンプル
任意の 1 文字 ? s?t は、sat や set を検出します。
文字からなる任意の文字列 * s*d は、sad や started を検出します。
単語の先頭 < <(inter) は興味深いとインターセプトを見つけますが、破片は見つかりません。
単語の末尾 > (in)> 内と内で検索しますが、興味深くはありません。
指定した文字のいずれか 1 つ [ ] w[io]n では、win と won が検出されます。
この範囲に含まれる任意の 1 文字 [-] [r-t]ight は右、視力、タイトを見つけます。 範囲は、昇順にする必要があります。
角括弧で囲まれた範囲の文字を除く任意の 1 文字 [!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 が検出されます。
直前の文字または式の 1 回以上の出現 @ 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();
});

特殊文字のSearch

// 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();
});

エスケープされた特殊文字にワイルドカードを使用するSearch

前の 「エスケープ特殊文字」で説明したように、正規表現で使用される特殊文字があります。 ワイルドカード検索でこれらの特殊文字の 1 つをプログラムで検索するには、'[' と ']' を使用してエスケープする必要があります。 この例では、ワイルドカード検索を使用して '{' 特殊文字を検索する方法を示します。

// 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 の詳細については、「Script Lab を使用して Office JavaScript API を探索する」を参照してください。

関連項目

詳細については、次のページを参照してください。