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

114
Visualizações
Find last index of element in string with regular and remove substring

I have an array of strings. i am using regular i am trying to find index of occurrence of element and remove substring from string but i have problem when i add another occurrence of character

My solution:

const names = [
  'COMMENT — CODE | COUNTRY | DATE | TARGET',
  'CODE| COUNTRY | DATE |TARGET',
  'CODE|| COUNTRY| DATE |TARGET',
  'COMMENT 1 — COMMENT 2 — CODE | COUNTRY | DATE | TARGET',
];

const exp = new RegExp(/—|-/);

const filteredNames = names.map(name => {
  const index = name.match(exp)?.index;
  return name.slice(index + 1)
});

My result:

"CODE | COUNTRY | DATE | TARGET"
"CODE| COUNTRY | DATE |TARGET"
"CODE|| COUNTRY| DATE |TARGET"
"COMMENT 2 — CODE | COUNTRY | DATE | TARGET" <- not correct

But expected result:

"CODE | COUNTRY | DATE | TARGET"
"CODE| COUNTRY | DATE |TARGET"
"CODE|| COUNTRY| DATE |TARGET"
"CODE | COUNTRY | DATE | TARGET"

I shall be most grateful for any help in this matter.

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

0

We could try a regex replacement approach here:

var names = [
  'COMMENT — CODE | COUNTRY | DATE | TARGET',
  'CODE| COUNTRY | DATE |TARGET',
  'CODE|| COUNTRY| DATE |TARGET',
  'COMMENT 1 — COMMENT 2 — CODE | COUNTRY | DATE | TARGET',
];
var output = names.map(x => x.replace(/(?:.*? — )*([^ |]+)/g, "$1"));
console.log(output);

Here is an explanation of the regex pattern being used here:

(?:.*? — )*  match (but do not capture) a "TERM — ", zero or more times
([^ |]+)     then capture the final term in $1

Then, we replace each pipe delimited entry with $1, the final term.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can just select part which you need:

const names = [
  'COMMENT — CODE | COUNTRY | DATE | TARGET',
  'CODE| COUNTRY | DATE |TARGET',
  'CODE|| COUNTRY| DATE |TARGET',
  'COMMENT 1 — COMMENT 2 — CODE | COUNTRY | DATE | TARGET',
];

const exp = new RegExp(/(\w+(\s*\|*\s*))*\w+\s*$/g);

const filteredNames = names.map(name => {
  const matches = name.match(exp);
  return matches?.[0] || '';
});

console.log(filteredNames);

about 4 years ago · Juan Pablo Isaza Relatório

0

To get the last element, you can split on either type of hyphen and get the last part:

const names = [
  'COMMENT — CODE | COUNTRY | DATE | TARGET',
  'CODE| COUNTRY | DATE |TARGET',
  'CODE|| COUNTRY| DATE |TARGET',
  'COMMENT 1 — COMMENT 2 — CODE | COUNTRY | DATE | TARGET',
];
const regex = /[—-]/;

names.forEach(s => {
  const parts = s.split(regex);
  console.log(parts[parts.length - 1].trim());
})

For a bit more performant match, you can use an optionally repeated group:

\w+(?:\s*\|+\s*\w+\s*)*$

Explanation

  • \w+ Match 1+ word chars
  • (?: Non capture group to repeat as a whole
    • \s*\|+\s*\w+\s* Match 1 or more pipes and word chars between optional whitspace cars
  • )* Optionally repeat
  • $ End of string

See a regex demo.

const names = [
  'COMMENT — CODE | COUNTRY | DATE | TARGET',
  'CODE| COUNTRY | DATE |TARGET',
  'CODE|| COUNTRY| DATE |TARGET',
  'COMMENT 1 — COMMENT 2 — CODE | COUNTRY | DATE | TARGET',
];
const regex = /\w+(?:\s*\|+\s*\w+\s*)*$/;

names.forEach(s => {
  const m = s.match(regex);
  if (m) {
    console.log(m[0])
  }
})

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