How to split a huge string by date using JavaScript?

穿精又带淫゛_ 提交于 2021-01-28 19:55:57

问题


I've got a pdf file turned into a huge string of over 1,000,000 characters. There are dates in the string in the format dd/mm/yyyy. I want to split the string by dates into smaller ones. I tried following:

var sectioned = hugeString.split(/^(0?[1-9]|[12][0-9]|3[01])[\/](0?[1-9]|1[012])[\/\-]\d{4}$/g);

But it's not working. I also tried hugeString.match(), but no good result there.

Is it even possible to accomplish this by string functions or should I think of a different approach?

String snippet:

....Section: 2 Interpretation E.R. 2 of 2012 02/08/2012 .....


回答1:


You may remove anchors, g modifier (it is redundant) and use non-capturing groups to avoid dates being output as well in the results. Wrap in (?=PATTERN HERE) if you need to split keeping the dates in the split chunks. However, if you prefer this approach, please make sure there are no optional 0s in the pattern at the beginning, or you might get redundant elements in the result.

var s = "....Section: 2 Interpretation E.R. 2 of 2012      02/08/2012 ..... ";
var res = s.split(/(?:0?[1-9]|[12][0-9]|3[01])[\/-](?:0?[1-9]|1[012])[\‌/-]\d{4}/);
console.log(res);
res = s.split(/(?=(?:0[1-9]|[12][0-9]|3[01])[\/-](?:0[1-9]|1[012])[\‌/-]\d{4})/);
console.log(res);

Note you also had a [\/] subpattern without - in the pattern while the other separator character class contained both chars. I suggest using [\/-] in both cases.



来源:https://stackoverflow.com/questions/40507682/how-to-split-a-huge-string-by-date-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!