Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 https://url.com/img.jpg]

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 https://url.com/img.jpg)

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

const str = "This is a string [img https://url.com/img.jpg]"

const url = str.match(/\[img ([^\]]+)\]/)[1];
console.log(url); // => https://url.com/img.jpg
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading