-1

I was working on the "Create an Editing Tool" project on the Google Applied Digital Skills website. When I was creating my editing tool, I came across two glitches. One is that the word "som" is highlighted, even though it isn't in the array. The other is that the word "heroi" is highlighted. This definitely looks like a bug, especially because "heroi" is highlighted in the same color as "hero."

Screenshot of Document

Does anyone know if this is an error I made or if it's a glitch? Here's the code:

    function findText(item) {

        //choose a random color as the background

        //credit to James from https://stackoverflow.com/questions/1484506/random-color-generator

        var background = '#' + (Math.random().toString(16) + "000000").substring(2, 8)

        //log the item being found to make sure it is being searched for

        Logger.log(item)

        //shows the computer what the search result is

        var searchResult

        //find the search result

        searchResult = DocumentApp.getActiveDocument().getBody().findText(item)

        //put it in the log

        Logger.log(searchResult)

        //loop until item is no longer found

        while (searchResult !== null) {

            //change the background color for a set space, which is when item is first used.

            searchResult.getElement().asText().setBackgroundColor(searchResult.getStartOffset(), searchResult.getEndOffsetInclusive(), background)

            //find the text again

            searchResult = DocumentApp.getActiveDocument().getBody().findText(item, searchResult)

            //end of the loop

        }



    }

    function highlightProblem() {

        //array showing all values of item

        var words = ["very", "Very", "totally", "Totally", "heroic", "Heroic", "really", "Really", "so ", "so. ", "so, ", "So ", "So. ", "So, ", "its", "Its", "good", "Good", "examples", "Examples", "hero ", "hero. ", "hero, ", "Hero ", "Hero. ", "Hero, "]

        //find each item in the array

        words.forEach(findText)

    }
Rubén
  • 34,714
  • 9
  • 70
  • 166
Isa
  • 1

1 Answers1

2

This is not a bug in Apps Script. The findText() function treats the string you are searching (Search Pattern) as a Regular Expression. In Regular Expressions, "." is a wildcard that matches any character. You have "so." and "hero." in your search patterns, thus matching "som" and "heroi".

You have a couple options, manually escape the . in your input array: "so\.", "hero\.", or use a a function to escape all the search expressions inside your findtext function.

There are examples of escape functions here: How to escape regular expression in javascript?

It's worth mentioning there are other advantages to allowing the use of Regular Expressions in your findText() function. For example, you can avoid having to duplicate words in Upper/lower case in your array. Instead you could pass in a single Regular Expression - "[Vv]ery" which would match "Very" or "very" or, /very/i which ignores case entirely. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

Cameron Roberts
  • 7,127
  • 1
  • 20
  • 32