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

292
Vistas
Typescript : Check for words in a string

There are many options in javascript, but are there any options in typescript to do the find words in a given string:

Eg:

For the list: ['Mr', 'Mrs', 'FM.', 'Sir'] and a string called 'Sir FM. Sam Manekshaw'. I have the words 'Sir' and 'FM.' present, hence needs to be assigned to a string and the remaining parts of the string assigned to another string. i.e:

a = Sir FM.
b = Sam Manekshaw

NOTE: It should validate only full words and not a substring.

UPDATE: What I tried:

var tempPrefixList = ['Mr', 'Mrs', 'FM.', 'Sir'];
var firstName = "Sir FM. Sam Manekshaw";

var prefixSearchExp = new RegExp(tempPrefixList.join("|"),"gi");
if(firstName && prefixSearchExp.test(firstName)) {
    console.log("Caught");
    var requestFirstNameSplit = firstName?.split(" ");
    console.log("Prefix: " + requestFirstNameSplit[0]);
    console.log("First name: " + requestFirstNameSplit[1]);
}

But this considers only if the name has one Prefix. And also, has only one name in firstName. Eg: Sir Sam. But doesn't work for the example I mentioned earlier.

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

0

You can solve it like this:

class PersonDetails {
  formOfAddress: string;
  name: string;

  constructor(formOfAddress: string, name: string) {
    this.formOfAddress = formOfAddress;
    this.name = name;
  }

  get fullName(): string {
    return [this.formOfAddress, this.name].join(' ');
  }
}

function extractPersonDetailsFromFullName(fullName: string): PersonDetails {
  const formOfAddresses = ['Mr', 'Mrs', 'FM.', 'Sir'];

  let usedFormOfAddresses = [];

  const wordsInName = fullName.split(' ').filter((x) => x);
  for (const word of wordsInName ?? []) {
    if (formOfAddresses.filter((foa) => foa == word).length == 1) {
      usedFormOfAddresses.push(word);
    }
  }

  const formOfAddress = usedFormOfAddresses.join(' ');

  return new PersonDetails(
    formOfAddress,
    fullName.substr(formOfAddress.length).trim()
  );
}

const testNames = ['Sir FM. Sam Manekshaw', 'Mr Tom'];

for (const testName of testNames) {
  const personDetails = extractPersonDetailsFromFullName(testName);
  appDiv.innerHTML +=
    testName +
    ' => <br>&nbsp;&nbsp;' +
    JSON.stringify(personDetails) +
    '<br><br>';
}

Code: https://stackblitz.com/edit/typescript-iyk3ul?file=index.ts

If you don't want to use the implementation with the separate function and object, you can use the following code:

const fullName = 'Sir FM. Sam Manekshaw';

const formOfAddresses = ['Mr', 'Mrs', 'FM.', 'Sir'];

let formOfAddressParts = [];
let nameParts = [];

const wordsInName = fullName.split(' ').filter((x) => x);
for (const word of wordsInName ?? []) {
  if (formOfAddresses.filter((foa) => foa == word).length == 1) {
    formOfAddressParts.push(word);
  } else {
    nameParts.push(word);
  }
}

let formOfAddress = formOfAddressParts.join(' ');
let name = nameParts.join(' ');

console.log('formOfAddress: ', formOfAddress);
console.log('name: ', name);
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