job.answiz.com
  • 1
Votes
name
name Punditsdkoslkdosdkoskdo

RegEx match with an exception in it

I search for a way to match with exceptions in it. For example I want to match ab-3/3/6, but I dont want to match ab-4/2/5:34. Also if ab-4/2/5:34 exists... I dont want to match it at all.

For this solution I build the following regex: ab-d{1,2}/d{1,2}/d{1,2}[^:]

My problem here is that my desired match only works if there are still characters behind them (like a whitespace, or a new line). So for example I have the following text:

ab-3/3/6
ab-4/2/5:34
ab-6/1/4

My match only contains ab-3/3/6.
That it dind't find ab-4/2/5 is what I want to archive.
But ab-6/1/4 is missing, because here is no character behind it.

Can someone please help me to finde a solution for this problem?

found this solution.
const dateStr = `ab-3/3/6
ab-4/2/5:34
ab-6/1/4`;

const matchAll = (str) => {
  let m;
  const reg = /ab-d{1,2}/d{1,2}/d{1,2}(?<!:)$/gm;
  let result = [];
  do {
    m = reg.exec(str);
    if (m) {
      result.push(m[0]);
    }
  } while (m);
  return result;
};
console.log(matchAll(dateStr));

 

  • 0
Reply Report


Related Questions

Trending Tags