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

Replace a part of string using replace method

我只想知道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}")`)