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

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

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 Report

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 Report

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