CSSファイルの内容を読み込み、外部ファイルへのすべてのリンクを探しています。私は速い正規表現を書いて、それを行うのが速くなると思ったが、これはイライラすることが分かっている。JavaScript - Regex - 一行に複数の一致がある場合、一致するパターンのみを取得します。
は私が持っていると言う:
@font-face{font-family:'FontAwesome';src:url('../fonts/fontawesome-webfont.eot?v=4.6.3');src:url('../fonts/fontawesome-webfont.eot?#iefix&v=4.6.3') format('embedded-opentype'),url('../fonts/fontawesome-webfont.woff2?v=4.6.3') format('woff2'),url('../fonts/fontawesome-webfont.woff?v=4.6.3') format('woff'),url('../fonts/fontawesome-webfont.ttf?v=4.6.3')
その行がやや多くのために行くが、ちょうど今のためにそれを取ることができます。
ご覧のとおり、src:url(...)
とurl(...)
の両方があるので、私はurl(..)
個のオカレンスを探すことができます。 テイク1:
var contents.match = ... // The contents of the .min.css file.
var matches = contents.match(/url\s*(.*);/); //no g at the end.
3を取る:
var contents.match = ... // The contents of the .min.css file.
var matches = contents.match(/url\s*(.*);/g);
は2テイク
var contents.match = ... // The contents of the .min.css file.
var matches = contents.match(/url\s*\((.*)\);/g);
4を取る:
var contents.match = ... // The contents of the .min.css file.
var matches = contents.match(/url\s*\((.*)\);/); // no g at the end.
ここ
は、私が試したものです210
私が欲しいのは、すべての一致の配列です。つまり、()または完全一致の文字列、つまりURL(...)です。代わりに、ファイル全体から大量のパイプを取得するだけです。
私が欲しいもの:
私は何かが欠けていますconsole.log(matches); /// Prints ["url(fonts/font_1.css')", "url('fonts/font_2.css')", "url('fonts/font_3.css')"]
の
console.log(matches); /// Prints ['fonts/font_1.css', 'fonts/font_2.css', 'fonts/font_3.css']
?
'*' 'マッチします;' '試合は最後まで延長することを意味している;'それが見つけました。 '。*'の代わりに '[^;] +'を試してみてください。 – Titus
注:[regex101](https://regex101.com/)のようなサイトは、正規表現をテストするときに非常に便利です(ちょうどそれに合わせてフレーバーを設定してください) –
'/url\((./?)\)/gを試してみてください' –