使用替换方法替换字符串的一部分



我只想知道js中的replace函数,它可以将下面的文本转换为另一个文本。

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

它作为字符串存储在变量数据中。在这个src需要被替换,它应该看起来像:

@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");
}

此外,https://cdn.jsdelivr.net/npm/@creative-fonts/alloy-ink/alloy-ink.otf存储在名为fonturl的变量中。

非常简单。

css.replace(/url(".+?")/, `url("${fonturl}")`)

最新更新