Technologies:
Tolerim
23 days ago
What is the method for obtaining this expression in Google Script?
As a Google Apps Script developer, I encountered an issue in obtaining a specific expression for a Google document. My aim is to locate and highlight all instances of words that conform to the format "X-X-Xr," where "X" represents a capital letter or number, "-" refers to the hyphen symbol, and "r" denotes the lowercase letter 'r'. Such words may exist between empty spaces or immediately after a colon (':').
Answers(2)
Vogi
22 days ago
Verified Answer
You can use Google Apps Script to find and highlight all instances of words that conform to the format "X-X-Xr," where "X" represents a capital letter or number, "-" refers to the hyphen symbol, and "r" denotes the lowercase letter 'r'. Such words may exist between empty spaces or immediately after a colon (':').
Here is an example code snippet that you can use:
function highlightWords() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var searchPattern = /(\b[A-Z0-9]-[A-Z0-9]-[A-Z0-9]r\b)|(\b[A-Z0-9]-[A-Z0-9]r\b)/g;
var text = body.editAsText();
var match;
while (match = searchPattern.exec(text.getText())) {
text.setBold(match.index, match.index + match[0].length - 1, true);
}
}
This code will find all instances of words that conform to the format "X-X-Xr" or "X-Xr" and will highlight them in bold. You can modify this code to suit your needs.
I hope this helps! Let me know if you have any other questions.
Sources
Search and use find and replace - Computer - Google Docs Editors HelpCan I color certain words in Google Document using Google Apps Script ...Google Docs Search entire document and bold wordsgoogle apps script - Finding text (multiple times) and highlighting ...Document Service | Apps Script | Google DevelopersTolerim
23 days ago
To find all occurrences of the specific word pattern "X-X-Xr" in a Google Doc via Google Apps Script, you can use the following regular expression:
var pattern = /\b\w-\w-\w\br\b/g;
This regular expression searches for the word pattern "X-X-Xr" where each X represents either a capital letter or number and the "-" and "r" are literal characters. The \b is a word boundary that ensures that the pattern is not matched as part of a longer word. The g flag is used to make the search global to find all occurrences within the document.
To then highlight these occurrences, you can use the setBackground method on the Range object in Google Apps Script:
var range = document.newRange(); // replace with the range you want to search/highlight
var text = range.getText();
var pattern = /\b\w-\w-\w\br\b/g;
var match;
while (match = pattern.exec(text)) {
var start = range.getStartOffset() + match.index;
var end = start + match[0].length;
range.setStart(text, start);
range.setEnd(text, end);
range.setBackground("yellow");
}
This code first gets the text within the given range and searches for the desired pattern with the regex. Then, for each match found, it calculates the start and end positions of the match within the range and sets the background color to yellow. Note that this code assumes you have already retrieved the document object, and you will need to adjust the range variable to specify the range you want to search and highlight.