2016-12-09 67 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?).+$