Microsoft Office加载项javascript:通过matchPrefix搜索:true-如何获取匹配前缀的完整单词

问题描述 投票:1回答:1

我想跟随文章here

此外,在此添加代码,因为链接总是可以移动或修改或下降。

    // Run a batch operation against the Word object model.
    Word.run(function (context) {

    // Queue a command to search the document based on a prefix.
    var searchResults = context.document.body.search('pattern', {matchPrefix: true});

    // Queue a command to load the search results and get the font property values.
    context.load(searchResults, 'font');

    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        console.log('Found count: ' + searchResults.items.length);

        // Queue a set of commands to change the font for each found item.
        for (var 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 by executing the queued commands, 
        // and return a promise to indicate task completion.
        return context.sync();
    });  
})
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

它工作正常,但我没有得到完整的回复。所以,如果这个词是patternABCDEFGH,那就是searchResults中的匹配词

var text = searchResults.items[i].text;
console.log('Matching text:' + text);

我得到的全部是pattern,我如何得到全文?

ms-word office-js office-addins
1个回答
1
投票

选项MatchPrefix不会搜索以该字母组合开头的每个单词 - 它只搜索字母组合时的字母组合。所以在这种情况下它只能找到字符“pattern”而不是整个“patternMatching”或“patternABCDEFGH”。

正如Juan所说,为了得到以某个字母组合开头的整个单词,你需要Word的正则表达式的变体:通配符搜索。

通配符模式如下所示:[P,p] attern *>

这假设您需要大写和小写“p”,后跟任何字符,直到单词结尾。

    var searchResults = context.document.body.search('[P,p]attern*>', {matchWildcards: true});
© www.soinside.com 2019 - 2024. All rights reserved.