How to separate out part of a string based on a pattern in typescript (probably using regex)

I have a string like this: This is a string [img

I want to take the string and separate out just the URL by searching for the "[img ]" pattern and taking the text within that (i.e. in the above, just the

Is there a way to do this with regular expressions? The "[]" are throwing me off on how to write one out.

>Solution :

All you need to do is escape it with \. Like this:

const str = "This is a string [img]"

const url = str.match(/\[img ([^\]]+)\]/)[1];
console.log(url); // =>

Leave a Reply