Match file name in <link href = "/ path / ????? / ????? / ????. Css"

You need to get the css file name from the link tag, which is located in a specific folder.

<link href="/assets/49f0ugdf8g/sub/style.css"   -> style.css

Currently

match(`<link .*?href="\/assets\/(.*?\.css)"/i)

Returns the path minus "/ assets /".
Can it be expanded to remove the rest of the path and just return the file name.

+3
source share
5 answers

It would be easier not to use a regex and use the built-in JS function String.split:

var link = document.getElementsByTagName('link')[0]; // or whatever JS to get your link element
var filename = link.href.split('/').pop(); // split the string by the / character and get the last part
+3
source

Of course:

match(<link .*?href="\/assets\/([^/]+\.css)"/i)
//                              ^^^^^ change here

?, , , , . +, * , .

0

Firstly, it should be warned .

Try the following: "\/assets\/(?:[^\/">]+\/)*([^\/">]*?.css)".

(?:...) - not an exciting group.

0
source

Try:

match(`<link .*?href="\/assets\/(?:[^\/]*\/)*(.*?\.css)"/i)
0
source
var link = '<link href="/assets/49f0ugdf8g/sub/style.css">';
var match = link.match(/<link .*?href="(.*)">/i);
var filename = match[1].split('/').pop();
0
source

Source: https://habr.com/ru/post/1781800/


All Articles