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

Replace a part of string using replace method

i just want to know the replace function in js to convert the below text to another.

@font-face {
  font-family: "Alloy Ink";
  font-style: normal;
  font-display: swap;
  src: url("alloy-ink.otf") format("opentype");
}

it is stored as a string in variable data. In this src needs to be replaced and it should look like:

@font-face {
  font-family: "Alloy Ink";
  font-style: normal;
  font-display: swap;
  src: url("https://cdn.jsdelivr.net/npm/@creative-fonts/alloy-ink/alloy-ink.otf")
    format("opentype");
}

Also https://cdn.jsdelivr.net/npm/@creative-fonts/alloy-ink/alloy-ink.otf is stored in a variable named fonturl.

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 :

Pretty simple.

css.replace(/url\(".+?"\)/, `url("${fonturl}")`)
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