Find last index of element in string with regular and remove substring

Viewed 40

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.

3 Answers

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.

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);

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])
  }
})

Related