Estoy tratando de extraer todos los hipervínculos de un archivo HTML grande usando javascript en la consola de línea de comandos de nodejs.
match() extrae los hipervínculos, pero también devuelve texto que no coincide después.
Por ejemplo:
Text before the link <a href="http:\\angst.org/frustration/pita1.html class=\"outlink\">PITA 1</a> the text after the link.`Resultado de la muestra:
<a href="http:\\angst.org/frustration/pita1.html class=\"outlink\">PITA 1</a> the text after the link.Curiosamente, cuando tomé una de las líneas/cadenas de textArray, la envolví en marcas de retroceso y la configuré en una variable, el código a continuación funcionó bien. Obtendría justo lo que quiero solamente:
<a href="http:\\angst.org/frustration/pita1.html class=\"outlink\">PITA 1</a>Este es el javascript que estoy usando:
// is an html file, input code removed for clarity of this example var text; // Split the html file into lines const textArray = text.split("\n"); // Regexp pattern to match the links var p = /<a href=\"http:\/\/angst.org\/frustration\/(.+)\.html\" class=\"outlink\">(.+)<\/a>/; // Extract and output all of the hyperlinks for (let i = 0; i < textArray.length; i++) { resultArray = textArray[i].match(p); console.log(resultArray[0]); }