How to locate text matching a regex pattern near cursor in Google Docs?

I’m trying to add hyperlinks to text in my Google Doc that matches a specific regex pattern. The pattern is supposed to find numbers with dashes, colons, semicolons, or commas. But for some reason, it’s not working as expected.

Here’s what I’m trying to do:

  1. Find text near the cursor that matches the pattern
  2. Add a hyperlink to that text

My code looks something like this:

function addLinkToMatch() {
  var doc = DocumentApp.getActiveDocument();
  var cursorArea = doc.getCursor().getSurroundingText();
  var matchedText = cursorArea.findText(/\d[-()\d:;,]+/);
  
  if (matchedText) {
    var start = matchedText.getStartOffset();
    var end = matchedText.getEndOffsetInclusive();
    var textToLink = matchedText.asText().getText().substring(start, end + 1).trim();
    
    // Add hyperlink logic here
  } else {
    // Handle case when no match is found
  }
}

The script runs without errors, but it’s not finding the text I expect it to. Am I using the wrong regex? Or is there a problem with how I’m searching for the text? Any help would be great!

I’ve encountered similar challenges with regex in Google Docs scripts. One thing to consider is that the findText() method can be finicky with complex patterns. Have you tried breaking down your regex into simpler components and then combining the results? For instance, you could search for numbers first, then check if they’re followed by the specified punctuation marks.

Another potential issue is the search scope. The getSurroundingText() method might be limiting your search area too much. Consider expanding your search to the entire document or at least a larger section around the cursor.

Lastly, don’t forget to account for different number formats. Your current regex might miss some valid cases. Testing with a variety of sample texts could help refine your pattern for better accuracy.

hey, i’ve had similar probs with regex in gdocs. maybe try using a simpler pattern first, like ‘\d+[-():;,]\d+’ and then expand from there? also, check if ur searching the whole doc or just a small part. sometimes the text u want is further away than u think. good luck!

I’ve dealt with similar issues in Google Docs scripts before, and I think I see what’s going on here. The problem is likely in how you’re using the findText() method.

In my experience, findText() doesn’t work well with complex regex patterns directly. Instead, I’ve had better luck using a simpler search and then refining the results.

Try this approach:

  1. Use findText() with a basic pattern like ‘\d+’ to find number sequences.
  2. Then, examine the surrounding text to see if it matches your full pattern.

Also, make sure you’re searching the entire document, not just the cursor area. Sometimes the match might be further away than you expect.

Lastly, don’t forget to use the ‘g’ flag in your regex to find all matches, not just the first one. It’s made a big difference in my scripts.

Hope this helps point you in the right direction!