Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
How can I retrieve the common substring between the end of a string and the beginning of another string?

Is there any simpler way (ideally from a builtin I missed from the doc) to retrieve the common substring between the end of a string and the beginning of another string? For example abcdef and defghi should return def.

abcdef
   defghi
=========
   def

My current implem:

const a = "abcdef";
const b = "defghi";

function findCommonEndStart(a, b) {
  let common = "";
  for (let i = 1; i <= a.length; i++) {
    const sub = a.slice(-i, a.length);
    if (b.startsWith(sub)) {
      common = sub;
    }
  }
  return common;
}

console.log(findCommonEndStart(a, b));

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Here how I did it.

let a = "abdef";
let b = "defdklc";

function func(x, y) {
  for (i in x) {
    if (x.substr(-i, x.length) == y.substr(0, i)) {
      console.log(x.substr(-i, x.length))
    }
  }
}

func(a, b)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Seems like you're looking for simple regular expression:

let str = 'abcdef defghi';
let find = str => str.match(/(\w+)\b.+\b(?:\1)/m)?.[1];

let result = find(str); // result = 'def'
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda