Home > OS >  Javascript use match() to select an entire paragraph
Javascript use match() to select an entire paragraph

Time:12-09

I'm trying to extract an entire paragraph from a txt file using JS, then put it to an html element. I've read quite a few posts already and articles, but I can't get the right regex formula. This is an extract of the txt file:

Doing initial required tests

   
   Testing server: xxxxxxxx\yyyyyyyy

      Starting test: Connectivity

         * Active Directory LDAP Services Check
         Determining IP4 connectivity 
         * Active Directory RPC Services Check
         ......................... yyyyyyyy passed test Connectivity



Doing primary tests


What I need to extract is from "Starting test: Connectivity" until "passed test Connectivity". This is the JS code I'm trying:

`

async function getText(file) {
  let dcdiagFile = await fetch(file);
  let dcDiag = await dcdiagFile.text();
  let connectivity = dcDiag.match(/Starting test: Connectivity[\n\r] $(.*)(/gm) || [];
  document.getElementById("DNS").innerHTML = connectivity;
  console.log(connectivity);
}
getText("./dcdiagreport.txt");

` With my code I only get the first line. I've thought that the wild card would address that, but if I add the word Connectivity as in below:

let connectivity = dcDiag.match(/Starting test: Connectivity[\n\r] $(.*)Connectivity (/gm) || [];

I don't get anything. In other programming languages I would go with "Text beginning * text ending" the paragraph. But not in JS. It's an intranet internetless servers and preferibly I don't want to download jquery. Vanilla JS only.

CodePudding user response:

You can use a .replace() instead of a .match() to keep only text you need.

You did not specify if you want to include or exclude the keys "Starting test: Connectivity" until "passed test Connectivity". Here is a test with regex1 that includes keys, and regex2 that excludes the keys:

const input = `Doing initial required tests
      Testing server: xxxxxxxx\yyyyyyyy

      Starting test: Connectivity

         * Active Directory LDAP Services Check
         Determining IP4 connectivity 
         * Active Directory RPC Services Check
         ......................... yyyyyyyy passed test Connectivity

Doing primary tests`;

const regex1 = /^.*?(Starting test: Connectivity.*?passed test Connectivity).*$/s;
let connectivity1 = input.replace(regex1, '$1');
console.log(connectivity1);

const regex2 = /^.*?Starting test: Connectivity\s*(.*?)\s*passed test Connectivity.*$/s;
let connectivity2 = input.replace(regex2, '$1');
console.log(connectivity2);

Explanation of regex1:

  • ^ -- anchor at start of text
  • .*? -- non-greedy scan
  • ( -- start of capture group 1
  • Starting test: Connectivity -- expect literal text
  • .*? -- non-greedy scan
  • passed test Connectivity -- expect literal text
  • ) -- end of capture group 1
  • .* -- greedy scan
  • $ -- anchor at end of text
  • /s -- modifier s to include newlines in .

Explanation of regex2:

  • similar to regex1, but capture group is more narrow
  • Related