Hier ist ein Beispiel dafür, wie man dies erreichen kann.
1) Da diese Eingabezeichenfolge:
const inputText =
`Text:"How secure is my information?"someRandomTextHere
Voice:"Not very much"
Text:"How to improve this?"
Voice:"Don't use '123456' for your password"
Text:"OK just like in the "Hackers" movie."`;
2) Extrahieren von Daten in doppelte Anführungszeichen nach dem Literal Text:
so dass die Ergebnisse ein Array mit allen Spielen wie folgt:
["How secure is my information?",
"How to improve this?",
"OK just like in the \"Hackers\" movie."]
LÖSUNG
function getText(text) {
return text
.match(/Text:".*"/g)
.map(item => item.match(/^Text:"(.*)"/)[1]);
}
console.log(JSON.stringify( getText(inputText) ));
RUN SNIPPET in einer Arbeits DEMO
const inputText =
`Text:"How secure is my information?"someRandomTextHere
Voice:"Not very much"
Text:"How to improve this?"
Voice:"Don't use '123456' for your password"
Text:"OK just like in the "Hackers" movie."`;
function getText(text) {
return text
.match(/Text:".*"/g)
.map(item => item.match(/^Text:"(.*)"/)[1]);
}
console.log(JSON.stringify( getText(inputText) ));
zu sehen, die nicht Lookbehind (oder sogar Look-Ahead). –
Wie sieht der Regex mit Lookbehind aus? – inetphantom
@MattBall, wie findest du das? Man könnte ein oder mehrere Zeichen mit einem Lookbehind für 'Text:' 'und einem Lookahead für' "' abgleichen. – henrebotha