Skip to content

Javascript extract strings that match regex from text

I’m trying to get a set of strings from a paragraph that match the format of 4chan’s quotes: >>1111111 where it starts with >> followed by 7 digits.

>>1111000
>>1111001
Yes, I agree with those sentiments. 

Both >>1111000 and >>1111001 would be extracted from the text above which I would then split into the digits after.

Answer

You can use the following which will match lines starting with 2 > characters followed by 7 digits:

const regex =/^[>]{2}[d]{7}$/gm;

const text = `>>1234567
>>6548789
foo barr`;

const matches = text.match(regex);

console.log(matches);