Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

293
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda