问题描述
我正在编写一个程序,以使用Javascript将文件系统中的路径与从SQL数据库中提取的URL进行匹配.提取的URL的结构如下:
I'm writing a program to match paths from my filesystem with urls pulled from an SQL database, using Javascript. The URLs pulled are structured like this:
http://examplesite.com/wp-content/uploads/YYYY/MM/17818380_1556368674373219_6750790004844265472_n-1.jpg
http://examplesite.com/wp-content/uploads/YYYY/MM/17818380_1556368674373219_6750790004844265472_n.jpg
https://examplesite.com/wp-content/uploads/YYYY/MM/10643960_909727132375975_2074842458_n-44x55.jpg
http://examplesite.com/wp-content/uploads/YYYY/MM/10643960_909727132375975_2078842458_n-320x150.jpg
等有些带有http,有些带有https.
etc. Some have http, some https.
我尝试将文件与网址匹配
I tried to match the files with the urls with
if(files[i] === urlsfromdb[j].substring(50,urlsfromdb[j].length-4))...
我想在... MM之后得到/
之后的所有内容,但是上面有时包含前导斜杠,这反过来会破坏程序.我如何使用正则表达式来完成此任务?我想获取所有的jpg,并且正在使用NPM glob.
I want to get everything after the /
after ...MM, but above sometimes includes the leading slash, which in turns ruins the program. How can I accomplish this with regexes? I wanna get all the jpgs, and I'm using NPM glob to do so.
此外,对于具有-WWWxHHH.jpg的文件(可能是2或3 Ws或Hs),我也想删除这些文件;数据库中的URL永远不会真正拥有它们,但文件将拥有它们.
Additionally, with the files that have -WWWxHHH.jpg, which could be 2 or 3 Ws or Hs, I want to delete those files as well; the URLS from the DB will never actually have them but the files will.
推荐答案
使用正则表达式删除到最后一个斜杠为止的所有内容.
use a regular expression to remove everything up to the last slash.
urlsfromdb[j].replace(/^.*\//, '')
这篇关于如何在不指定固定长度的情况下使用Javascript中的正则表达式来截取部分网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!