0
votes

Good afternoon,

Would like to know if it is possible to search a word but ignore the same word with other word in regex.

Example : One : Two : Three : Four : Five : Six : Two Three : Nine

Answer : One : Two : Three : Four : Five : Six : Two Three : Nine

So the regex only found the first word Three and ignore Two Three.

I tried this regex :

(?!Two Three)(Three)

but it doesn't work, it does work if the word is two

Any help or suggestions i'd gladly appreciate. Thanks

2
So you just want to take the word when it's between double dots ?André DS
Not really because it can start or finish with Three (ex : Three : One : Two : [...] : Three)user3869659

2 Answers

1
votes

With the input One : Two : Three : Four : Five : Six : Two Three : Nine

in which you want to ignore Two Three you can do

let str = 'One : Two : Three : Four : Five : Six : Two Three : Nine';
let re = /Two(?!\sThree)/g;
console.log(str.match(re));
1
votes

If you are searching for something more generic I suggest a pure js approach:

let str = 'One : Two : Three : Four : Five : Six : Two Three : Nine';

let resp = str
  .split(' : ')
  .reduce((acc, ele) => acc.some(x => ele.search(x) > 0) ? acc : acc.concat(ele), [])
  .join(' : ');
  
console.log(resp);