1

I am matching three patterns described below, they all are independent. Follow the hyperlinks to see demonstration in regex playground.


Pattern: /(?<=^[^.]*\.[^.]*)\..*/g, matches everything beginning from second ..


Pattern: /(?<=\.\d{2}).*/g, for input characters that are /[0-9.]/g only, matches everything beginning from 3rd digit after first ..


Pattern: /(?<=\.\d{4}).*/g, for input characters that are /[0-9.]/g only, matches everything beginning from 5th digit after first ..


I am not able to do this without using lookbehind in JS.

Haslo Vardos
  • 322
  • 2
  • 10

1 Answers1

1

What I'd do is use groups and grab the last group.

let r1 = /(\.)[^.]*(\.)(.*)/g;

let m1 = r1.exec('..post dots match');
console.log(m1[m1.length-1]);

let r2 = /(\.\d{2})(.*)/g;

let m2 = r2.exec('.001234');
console.log(m2[m2.length-1]);

let r3 = /(\.\d{4})(.*)/g;
let m3 = r3.exec('.123400000');
console.log(m3[m3.length-1]);
Matt Ellen
  • 11,268
  • 4
  • 68
  • 90