Aquí es un ejemplo que muestra cómo se puede abordar esto.
1) Habida cuenta de esta cadena de entrada:
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) extraer datos en comillas dobles después de la literal Text:
para que los resultados es una matriz con todos los partidos, así:
["How secure is my information?",
"How to improve this?",
"OK just like in the \"Hackers\" movie."]
SOLUCIÓN
function getText(text) {
return text
.match(/Text:".*"/g)
.map(item => item.match(/^Text:"(.*)"/)[1]);
}
console.log(JSON.stringify( getText(inputText) ));
RUN SNIPPET para ver un DEMO DE TRABAJO
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) ));
eso no es de búsqueda hacia atrás (o incluso de búsqueda hacia delante). –
¿Cómo sería la expresión regular con lookbehind? – inetphantom
@MattBall, ¿cómo te imaginas? Uno podría coincidir en uno o más personajes con un lookbehind para 'Text:' 'y un lookahead para' "'. – henrebotha