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

148
Visualizações
How do I extract a certain portion of a string and make it clickable in Javascript?

I have a JS string of this form:

const pageContent = "He stood up and asked the teacher, "Can you elaborate the last point please? ".

I want to map the words to a page and represent it in a view such that the words are clickable. Is there a way to be extract portions of the string using the word index such that any punctuation marks occuring before and after any words are not clickable but the words are? For instance the word Can is clickable but not "Can?

PS: I cannot use the split function to convert the string into an array and map it as this can lead to failure in some edge cases and alters the text flow when converted back into a string. I have the indexes of all the words in a separate array with the starting and end index of each word in the string and want to use these.

Any help is highly appreciated

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Simple use two for loops and check if the word matches any of the words in the excluded words array

const pageContent = 'He stood up and asked the teacher, "Can you elaborate the last point please?", Can you elaborate the last point please? '
const indexes = [
  [35, 39],
  [9, 11]
]
const excluded_words = ['"Can']
for (let i = 0, len = indexes.length; i < len; i++) {
  const word = pageContent.substring(indexes[i][0], indexes[i][1]);

  for (let i = 0, len = excluded_words.length; i < len; i++) {
    if (word.indexOf(excluded_words[i]) > -1) {
      console.log(word, " is excluded");
      break;
    }
    if (i === len - 1) {
      console.log(word, " is included")
    }
  }
}

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