Home > Enterprise >  regular expression to get only letters between a look behind and look ahead
regular expression to get only letters between a look behind and look ahead

Time:01-22

I am trying to extract only the letters from "130b2c94-22ba-4584-b18a-899eb4be045d"

<input type="hidden" name="logonInfo.forceAuthn" value="false" id="sSignOnauthenticateUser_logonInfo_forceAuthn"/>
<input type="hidden" name="logonInfo.inResponseTO" value="_130b2c94-22ba-4584-b18a-899eb4be045d" id="sSignOnauthenticateUser_logonInfo_inResponseTO"/>
<input type="hidden" name="logonInfo.issuerUrl" 

I have tried

(?<=name="logonInfo.inResponseTO" value="_).*(?=" id="sSignOnauthenticateUser)

to extracts the string I need, but I need to break it down further to only the letters

CodePudding user response:

Use a DOM Parser. This will reliably interpret the HTML and give you a DOM.

Then use a CSS selector query to find the input elements that have a value attribute which has a value that starts with underscore.

Finally remove all non-letters in that found value using a regex:

const html = `
<input type="hidden" name="logonInfo.forceAuthn" value="false" id="sSignOnauthenticateUser_logonInfo_forceAuthn"/>
<input type="hidden" name="logonInfo.inResponseTO" value="_130b2c94-22ba-4584-b18a-899eb4be045d" id="sSignOnauthenticateUser_logonInfo_inResponseTO"/>
<input type="hidden" name="logonInfo.issuerUrl" >`;

const doc = new DOMParser().parseFromString(html, "text/html");
for (const input of doc.querySelectorAll("input[value^=_]")) {
  const letters = input.value.replace(/[^a-z]/gi, "");
  console.log(input.name, ":", letters);
}

  • Related