Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

230
Views
regex from Javascript to google sheets

I'm trying to get this regex to google sheets. Wrap Text In JavaScript

In the solution

const wrap = (s) => s.replace(
    /(?![^\n]{1,32}$)([^\n]{1,32})\s/g, '$1\n'
);

I'm trying to replace this function with the native REGEXREPLACE to make it run faster. I have tried a few combinations but it all gives me the Function REGEXREPLACE parameter 2 value " /(?![^\n]{1,32}$)([^\n]{1,32})\s/g" is not a valid regular expression. with =REGEXREPLACE(AP18," /(?![^\n]{1,32}$)([^\n]{1,32})\s/g","$1\n") error.

I have also tried: =REGEXREPLACE(AP18,"(?![^\n]{1,32}$)([^\n]{1,32})\s","$1\n")

same result

Can I not move it to a native Google function? or is there something wrong with my regex?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You have to use a quirky workaround when using REGEXREPLACE with newline in the replacement part: The "\n" newline does not work, so you have to enter a literal newline:

=REGEXREPLACE(D$7,"([^\n]{32}[^\s]{1,32})","$1
")

To do that, place the cursor to the right place in the replacement part, and hit CTRL+ENTER.

Note that I don't use a lookahead. That is because built-in functions only support the RE2 regex syntax, not the full one of the V8 JavaScript engine.

Alternatively, create a custom function under Extension => Apps Script. Here are two variants:

function WRAP1(str) {
  return str.replace(/(?![^\n]{1,32}$)([^\n]{1,32})\s/g, '$1\n');
}

function WRAP2(str) {
  return str.replace(/([^\n]{32}[^\s]{1,16})/g, '$1\n');
}

I prefer the latter because it wraps a superSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLongWordToo.

about 4 years ago · Santiago Trujillo Report

0

The RE2 regex engine used by Google Sheets regex functions purposefully omits look-ahead and look-behind. That means that the (?!...) assertion is not supported by regexreplace().

It is usually possible to write an alternative regex that does not use those features. A full solution may involve several nested regex functions.

To learn the exact regular expression syntax used by the regex functions in Google Sheets, see RE2.

Note that the query() spreadsheet function uses Perl Compatible Regular Expressions that do support look-ahead and look-behind. Also note that Google Apps Script uses JavaScript regexes that support those assertions as well.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!