수정해야합니다. pdf의 모든 텍스트를 추출하기 위해 찾은이 코드를 사용하고 있습니다.
<!-- edit this; the PDF file must be on the same domain as this page -->
<iframe id="input" src="your-file.pdf"></iframe>
<!-- embed the pdftotext service as an iframe -->
<iframe id="processor" src="http://hubgit.github.com/2011/11/pdftotext/"></iframe>
<!-- a container for the output -->
<div id="output"></div>
<script>
var input = document.getElementById("input");
var processor = document.getElementById("processor");
var output = document.getElementById("output");
// listen for messages from the processor
window.addEventListener("message", function(event){
if (event.source != processor.contentWindow) return;
switch (event.data){
// "ready" = the processor is ready, so fetch the PDF file
case "ready":
var xhr = new XMLHttpRequest;
xhr.open('GET', input.getAttribute("src"), true);
xhr.responseType = "arraybuffer";
xhr.onload = function(event) {
processor.contentWindow.postMessage(this.response, "*");
};
xhr.send();
break;
// anything else = the processor has returned the text of the PDF
default:
output.textContent = event.data.replace(/\s+/g, " ");
break;
}
}, true);
</script>
출력은 단락없이 압축 된 텍스트입니다. 내 모든 pdf에는 처음 어딘가에 'Datacover'라는 단어가 있으며 큰 단락 뒤에옵니다.
내가 원하는 것은 'Datacover'라는 단어의 첫 번째 인스턴스까지 처음부터 모든 텍스트를 삭제하고 'Datacover'라는 단어의 앞부분에서 '의 세 번째 인스턴스까지 모든 텍스트를 표시하는 것입니다. '<-(공백이있는 점) 끝까지 다음 텍스트를 모두 삭제합니다.
도울 수 있니? 감사!
단어 경계 사이에 Datacover를 일치시키고 점과 공백이 다음에 나타날 때까지 \b
newling을 포함하여 모든 문자와 일치하는 욕심이없는 방식으로 3 번 반복 할 수 있습니다.[\s\S]*?
\.
\bDatacover\b(?:[\s\S]*?\. ){3}
데이터를 얻으려면 다음을 사용할 수 있습니다.
event.data.match(regex)
예를 들면 :
const regex = /\bDatacover\b(?:[\s\S]*?\. ){3}/g;
let event = {
data: `testhjgjhg hjg jhg jkgh kjhghjkg76t 76 tguygtf yr 6 rt6 gtyut 67 tuy yoty yutyu tyu yutyuit iyut iuytiyu tuiyt Datacover uytuy tuyt uyt uiytuiyt uytutest.
yu tuyt uyt uyt iutiuyt uiy
yuitui tuyt
test.
uiyt uiytuiyt
uyt ut ui
this is a test.
sjhdgfjsa.
hgwryuehrgfhrghw fsdfdfsfs sddsfdfs.`
};
console.log(event.data.match(regex));
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다