2016-12-09 63 views
0

我正在閱讀PDF文件。在PDF文件的一頁中,我想從該字符串行中獲取日期「插圖截至:2016年12月9日」。我能夠使用正則表達式來搜索「插圖爲:」。但我怎麼不能得到日期。我需要返回正則表達式的結果。我不需要返回我搜索的相同字符串。如何在字符串匹配時在行尾獲得下一個字符串

i tried this but it only return me Illustration as of : 

const regex = /Illustration as of:/g; 
const str = `Illustration as of: December 9, 2016`; 
let m; 

while ((m = regex.exec(str)) !== null) { 
    // This is necessary to avoid infinite loops with zero-width matches 
    if (m.index === regex.lastIndex) { 
     regex.lastIndex++; 
    } 
    // The result can be accessed through the `m`-variable. 
    m.forEach((match, groupIndex) => { 
     console.log(`Found match, group ${groupIndex}: ${match}`); 
    }); 
} 

我只需要回到這個結果2016年12月9日

+0

'(:插圖爲:??\ S)(一月|二月|三月|五月|六月|七月|八月|九月|十月|十一月|十二月)\ d {1, 2},\ s?\ d {4}'或'(?:圖示爲:\ s?)。+ $' –

+0

它看起來很複雜。 –

+0

第二個很簡單,只要找'Illustraton作爲:'並且把它放在後面直到行結束。 –

回答

0
const regex = /Illustration as of:+([^\n]+)/g; 

試試這個,我希望它會回到你剛剛日期。它會在比賽結束後返回字符串的結尾。

+0

感謝它對我有用... Hattsoff –

0

(?:Illustration as of:\s?)(January|February|March|May|June|July|August|Septembe‌​r|October|November|D‌​ecember) \d{1,2},\s?\d{4}(?:Illustration as of:\s?).+$