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

289
Views
Compruebe si la palabra en la oración también está en la matriz

Si la palabra en la oración también está en la matriz, tome las primeras 3 letras de esta palabra y ponga un punto al final.

 var arr = ['select', 'delete', 'Truncate', 'insert', 'update'] function checkValue(value, arr) { var newText; for (var i = 0; i < arr.length; i++) { var name = arr[i]; if (value.toLowerCase().includes(name.toLowerCase())) { newText = value.toLowerCase().replace(name, name.substring(0, 3)); break; } } return newText; } console.log(checkValue('Hello Hello Delete33 Hello', arr));

Me está dando: hola hola del33 hola.

Pero quiero : Hola Hola Del. Hola.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Puede crear una expresión regular que coincida con sus elementos arr después de un límite de palabra y consumir cualquier carácter de palabra después de ellos, y reemplazar con los primeros tres caracteres agregando un punto justo después:

 var arr = ['select', 'delete', 'Truncate', 'insert', 'update'] var re = new RegExp("\\b(?:" + arr.join('|') + ")\\w*", 'ig') function checkValue(value, re) { return value.replace(re, (x) => x.substr(0,3) + "."); } console.log(checkValue('Hello Hello Delete33 Hello', re)); // => Hello Hello Del. Hello

Vea la demostración de expresiones regulares . Detalles :

  • \b - un límite de palabra
  • (?:select|delete|Truncate|insert|update) - un grupo que no captura que coincide con una de las palabras
  • \w* - cero o más caracteres de palabra (letras, dígitos, _ ).
about 4 years ago · Juan Pablo Isaza Report

0

 var arr = ['select', 'delete', 'truncate', 'insert', 'update'] function checkValue(value, arr) { return value.split(" ").map(x => arr.find(y => x.toLowerCase().includes(y)) ? x.substring(0, 3) + "." : x).join(" "); } console.log(checkValue('Hello Hello Delete33 Hello', arr));

about 4 years ago · Juan Pablo Isaza Report

0

La clave es que está obteniendo la subcadena pero reteniendo los números y no agregando un punto.

Aquí hay un método alternativo usando map , some y join .

 const arr = ['select', 'delete', 'Truncate', 'insert', 'update']; function checkValue(value, arr) { // `split` the string into words, and `map` // over them return value.split(' ').map(word => { // If one of the lowercase words in the array // is included included in the lowercase word... const found = arr.some(el => { return word .toLowerCase() .includes(el.toLowerCase()); }); // ...return the updated word if (found) return `${word.slice(0, 3)}.`; // Otherwise just return the word return word; // And `join` the array back to a string }).join(' '); } const str = 'Hello Hello Delete33 Hello Truncate'; const out = checkValue(str, arr); console.log(out);

Documentación adicional

  • Plantilla/literales de cadena
about 4 years ago · Juan Pablo Isaza 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!