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

138
Views
¿Cómo dividir una cadena incluyéndola en la matriz javascript?

Digamos que tenemos las siguientes cadenas:

 const str1 = 'aabbcc'; const str2 = 'aabbccaaddaaaaeeff';

Necesito dividirlos para obtener el siguiente resultado:

 mySplitFunction(str1, 'aa')//<--- ['aa','bbcc'] mySplitFunction(str1, 'bb')//<--- ['aa','bb', 'cc'] mySplitFunction(str2, 'aa')//<--- ['aa','bbcc', 'aa','dd', 'aa','aa', 'eeff'] mySplitFunction(str2, 'dd')//<--- ['aabbccaa','dd', 'aaaaeeff']

¿Como lo harias?

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

0

Probar:

 const str1 = 'aabbcc'; const str2 = 'aabbccaaddaaaaeeff'; function mySplitFunction(str, delimiter){ return str.split(new RegExp(`(${delimiter})`)).filter(s => s) } console.log(mySplitFunction(str1, 'aa')) console.log(mySplitFunction(str1, 'bb')) console.log(mySplitFunction(str2, 'aa')) console.log(mySplitFunction(str2, 'dd'))

about 4 years ago · Juan Pablo Isaza Report

0

Puede tomar el separador entre paréntesis y filtrar el resultado para omitir cadenas vacías.

 const split = (string, separator) => string .split(new RegExp(`(${separator})`)) .filter(Boolean), str1 = 'aabbcc', str2 = 'aabbccaaddaaaaeeff'; console.log(...split(str1, 'aa')); // ['aa','bbcc'] console.log(...split(str1, 'bb')); // ['aa','bb', 'cc'] console.log(...split(str2, 'aa')); // ['aa','bbcc', 'aa','dd', 'aa','aa', 'eeff'] console.log(...split(str2, 'dd')); // ['aabbccaa','dd', 'aaaaeeff']

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!