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.

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.


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
foo barr`;

const matches = text.match(regex);