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

224
Views
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 answers
Answer question

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 Report

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 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!