亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取字符前的部分子串

獲取字符前的部分子串

拉丁的傳說 2023-05-25 15:35:45
我有一個網址,像這樣:https://www.example.com/exampletitle21sep11oct2020/index.html我需要的部分是在最后一個和倒數第二個“/”字符之間。但我不需要整個部分,我特別需要最后一個“/”字符之前的最后日期。如您所見,有兩個日期緊挨著,它們之間沒有分隔符,因此很難使用substring或indexOf方法。更困難的是,第一個日期只包含日和月,而最后一個日期包含整個日期。有什么方法可以讓我從此網址中提取最后一個“/”字符之前的最后日期嗎?
查看完整描述

4 回答

?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

使用正則表達式,您可以獲得第二個日期,如下所示:


const regex = /\/(?:.*?(\d{1,2}\w{3}\d{0,4}))\/.*?$/;


const [, date] = regex.exec("https://www.example.com/exampletitle21sep11oct2020/index.html");

console.log({ date })


const regex = /\/(?:.*?(\d{1,2}\w{3}\d{0,4}))\/.*?$/;


const [, date] = regex.exec("https://www.example.com/exampletitle21sep9oct2020/index.html");

console.log({ date });

console.log(regex.exec("https://www.example.com/exampletitle21sep9oct/index.html")[1])


查看完整回答
反對 回復 2023-05-25
?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

您可以找到并解析包含以下模式的路徑:


^? ? ? ? ?Line start

.+? ? ? ? One or more of anything

(\d{2})? ?2-digit date

(\w{3})? ?3-letter month (lowercase)

(\d{2})? ?2-digit date

(\w{3})? ?3-letter month (lowercase)

(\d{4})? ?4-digit year

$? ? ? ? ?Line end

例子

我用moment來處理日期解析。



const expression = /^.+(\d{2})(\w{3})(\d{2})(\w{3})(\d{4})$/;

const format = 'DD MMM YYYY';

const toTitleCase = (str) => str.charAt(0).toUpperCase() + str.slice(1);


const parseDates = (path) => {

? const url? ? = new URL(path),

? ? ? ? tokens = url.pathname.split('/'),

? ? ? ? found? = tokens.find(token => token.match(expression));

? if (!found) return null;

? const [

? ? , startDate, startMonth, endDate, endMonth, year

? ] = found.match(expression);

? return {

? ? start : moment(`${startDate} ${toTitleCase(startMonth)} ${year}`, format),

? ? end? ?: moment(`${endDate} ${toTitleCase(endMonth)} ${year}`, format)

? };

};


const dates = parseDates('https://www.example.com/exampletitle21sep11oct2020/index.html');


console.log(dates);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

展開片段


查看完整回答
反對 回復 2023-05-25
?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊

僅使用一個正則表達式,一切都會簡單得多:


var url = 'https://www.example.com/exampletitle21sep11oct2020/index.html'


var res = url.match( /.*?(\d+[a-z]+\d{4})\/.*?$/i );

// res === [ "https://www.example.com/exampletitle21sep11oct2020/index.html", "11oct2020" ]

var endDate = res[1];

// endDate === "11oct2020"

或(但“exampletitle”不得以數字結尾):


var res = url.match( /.*?(\d+[a-z]+)(\d+[a-z]+)(\d{4})\/.*?$/i );

// [ "https://www.example.com/exampletitle21sep11oct2020/index.html", "21sep", "11oct", "2020" ]

或者:


var res = url.match( /.*?(\d+)([a-z]+)(\d+)([a-z]+)(\d{4})\/.*?$/i );

// [ "https://www.example.com/exampletitle21sep11oct2020/index.html", "21", "sep", "11", "oct", "2020" ]

但是,如果您知道日期始終是 2 位數字(始終是“01”,而不是“1”),則“exampletitle”可以是任何字符串:


var res = url.match( /.*?(\d{2}[a-z]+\d{4})\/.*?$/i );

var res = url.match( /.*?(\d{2}[a-z]+)(\d+[a-z]+)(\d{4})\/.*?$/i );

var res = url.match( /.*?(\d{2})([a-z]+)(\d+)([a-z]+)(\d{4})\/.*?$/i );


查看完整回答
反對 回復 2023-05-25
?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

試試這個更新


const url = "https://www.example.com/exampletitle21sep11oct2020/index.html";

const urlData = url.split('/');

const datePart = urlData[urlData.length-2];

const res = datePart.slice(-9); <-- this will give you "11oct2020" -->


查看完整回答
反對 回復 2023-05-25
  • 4 回答
  • 0 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號