2012-12-21 20 views
0

使用Flickr API。如何僅匹配一個句子中的數字

result.items[i].published 

結果像這樣2012-12-21T14:36:24Z - 注意T和Z? 我如何只匹配2012-12-21T14:36:24Z中的數字?

這就是它的樣子。

newPic.innerHTML = "<div class='pic-container'> <p class='title'>" + result.items[i].title + "</p> <a href="+ result.items[i].link +" > <img src=\'" + result.items[i].media.m + "\'> </a></div><p class='author'>" + result.items[i].author.match(/\(([^)]*)\)/)[1] + "</p><p class='published'>" + result.items[i].published + "</p>"; 
+0

你可以給我們更多的細節,你想要達到什麼?我不確定基於此提供的答案。你是否試圖打印沒有T和Z的日期?或者是其他東西 ? – Aviatrix

+4

'新日期(result.items [i] .published)' - 那麼你應該能夠拿出領域? – nhahtdh

+0

@Aviatrix正是!我試圖打印沒有T和Z的日期。 如果你喜歡,你可以在www.johancarleborn.se看看它的意思。 –

回答

0

您可以使用正則表達式匹配「\ d」爲這一點,但我認爲一個更好的解決辦法是用

var d = new Date(result.items[i].published) 

從你得到一個正確的Date object解析日期。

0

你可以使用一個簡單的正則表達式,如:

result.items[i].published.replace(/[a-z]/i , ""); 

,或者如果你想用空格來代替字母:

result.items[i].published.replace(/[a-z]/i , " "); 

我提供的服務將在您的字符串匹配任何字母的兩個例子替換它們,如果你想匹配更多的字符而不僅僅是字母,你可以簡單地修改正則表達式。

希望這回答了你的問題,

納齊奧西隆

0

因爲JS日期對象是地獄的工作,最好的解決辦法是運行一個簡單的正則表達式替換字符串。

result.items[i].published.replace(/Z|T/gi," ")

如果你需要的工作,並做一些日期先進的東西我給你推薦dateJS庫。