1

I am creating a component for a CMS that runs a find/replace on the response of text input, it should replace any instances of [html_image url="example"] with

I have managed to create a simple string.replace that successfully replaces a single instance (my regex is an exact match), however, I am looking for a little help refactoring my regex to find ALL instances.

article.content = "this is an image, {html_image id=222}. This is also an image {html_image id=111}

article.content = article.content.replace(
      new RegExp('{html_image id=222}'),
      '<img src="" />'
    );

current output = "this is an image, <img src="222"/>. This is also an image {html_image id=111}"
expected output = "this is an image, <img src="222"/>. This is also an image <img src="111"/"
1
  • new RegExp('{html_image id=222}', 'g') add the global tag with your regex Commented Feb 13, 2020 at 11:04

1 Answer 1

3

You can use the following code (adapt it to your variables):

var content = "this is an image, {html_image id=222}. This is also an image {html_image id=111}";
content = content.replace(
  /{html_image id=(\d*)}/g,
  '<img src="$1" />'
);
console.log(content);

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.