0

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.

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.