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

283
Views
Google Apps Script: TextFinder regex no encuentra espacios finales

Tengo una columna en Hojas de cálculo con nombres de empresas donde quiero eliminar texto como inc., llc, etc. Puede variar considerablemente donde algunos tendrán inc con o sin un punto al final, a veces con una coma, y puede caer tanto al final de la cadena como en el medio, así que creé los bucles anidados en lugar de buscar cada iteración explícitamente.

La declaración if existe para el texto que tiene un espacio inicial porque, en esos casos, quiero reemplazarlo con un espacio.

El siguiente código funciona, excepto cuando hay un espacio final, por ejemplo: " inc " o ", inc ". Si pongo algo explícito como searchTerm = range.createTextFinder("inc"); no hay problema

El problema parece estar en la cadena concatenada. Intenté reemplazar el " " en la matriz de puntuación con "\\s", pero eso aún no funciona.

¿Puede alguien ayudarme a ver lo que me estoy perdiendo aquí?

 function dataClean(range) { var endings = ["inc", "ltd", "llc", "llp", "lp", "lcc"]; var punctuationStart = [", ", " "]; var punctuationEnd = [".", " ", "$"]; var searchTerm, regex; /*Loops through endings with the pattern: ', inc.' ', inc ' ', inc$' ' inc.' ' inc ' ' inc$'*/ for (i=0; i<endings.length; i++){ for (j=0; j<punctuationStart.length; j++){ for (k=0; k<punctuationEnd.length; k++){ regex = punctuationStart[j] + endings[i] + punctuationEnd[k]; /*k==1 is used for trailing spaces so they are replaced with a space*/ if (k==1){ searchTerm = range.createTextFinder(regex); searchTerm.useRegularExpression(true); searchTerm.matchCase(false); searchTerm.replaceAllWith(" "); } else{ searchTerm = range.createTextFinder(regex); searchTerm.useRegularExpression(true); searchTerm.matchCase(false); searchTerm.replaceAllWith(""); } } } } } ```
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

No soy un experto en expresiones regulares, pero siempre que el sufijo esté al final del nombre de la empresa, funcionará. Si está en el medio, tendrá que romper la cuerda, extraer el sufijo y volver a armarla.

 function testRegex() { let test = "My Company, llc."; let match = test.match(/(\s|,\s)(inc|ltd|llc|lp|lcc)/); console.log(match); console.log(test.substring(0,match.index)); } 11:27:53 AM Notice Execution started 11:27:53 AM Info [ ', llc', ', ', 'llc', index: 10, input: 'My Company, llc.', groups: undefined ] 11:27:53 AM Info My Company 11:27:53 AM Notice Execution completed

He probado la expresión regular para los siguientes escenarios en RegExr

ingrese la descripción de la imagen aquí

about 4 years ago · Santiago Gelvez 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!