0

I am trying to extract the value of id from the code provided below

I have tried the following regular expression but it still comes back as my default value of : id_not_found

id" selectNOIOrg_do_frm_organization="(.+?)" />

<input type="radio" name="frm.organization" id="selectNOIOrg_do_frm_organization{C5DF28FD-26EF-90DA-1214-BD72E0214F17}" value="{C5DF28FD-26EF-90DA-1214-BD72E0214F17}" title="College of St. Jude" ext-ns-multiple="frmorganization">

I expect the regex extractor to be able to recognize the id (it is a dynamic id and changes based on the radio button selected)

Emma
  • 27,428
  • 11
  • 44
  • 69
owais khan
  • 19
  • 5
  • added a code snippet that shows that the regex is correct. can you add the code you use to execute your regex? – Michael May 23 '19 at 15:45

3 Answers3

0

Here, we might just want to take id=" as a left boundary and " as a right boundary, then collect our attribute value in the first capturing group $1:

id="(.+?)"

enter image description here

DEMO

Demo

This snippet just shows that how the capturing groups work:

const regex = /id="(.+?)"/gm;
const str = `<input type="radio" name="frm.organization" id="selectNOIOrg_do_frm_organization{C5DF28FD-26EF-90DA-1214-BD72E0214F17}" value="{C5DF28FD-26EF-90DA-1214-BD72E0214F17}" title="College of St. Jude" ext-ns-multiple="frmorganization">
`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

RegEx

If this expression wasn't desired, it can be modified or changed in regex101.com.

RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

Community
  • 1
  • 1
Emma
  • 27,428
  • 11
  • 44
  • 69
0

You can use id="\w+{([A-Z0-9-]+)}", if the string before the id could change.

If the string before the id is always the same or there are multiple id-strings like that and you only want this specific one use `

let html = '<input type="radio" name="frm.organization" id="selectNOIOrg_do_frm_organization{C5DF28FD-26EF-90DA-1214-BD72E0214F17}" value="{C5DF28FD-26EF-90DA-1214-BD72E0214F17}" title="College of St. Jude" ext-ns-multiple="frmorganization">';
let rgx = /id="(selectNOIOrg_do_frm_organization{([A-Z0-9-]+)})"/;

var result = rgx.exec(html);
if (result) {
    alert('regex matched:\n\nfull-id=' + result[1] + '\n\nvalue=' + result[2]);
} else {
    alert('regex does not match');
}

`

To match only GUIDs as IDs you can use id="selectNOIOrg_do_frm_organization{([A-Z0-9-]{8}-[A-Z0-9-]{4}-[A-Z0-9-]{4}-[A-Z0-9-]{4}-[A-Z0-9-]{12})}"

Emma
  • 27,428
  • 11
  • 44
  • 69
Michael
  • 1,931
  • 2
  • 8
  • 22
0

In the pattern you tried id" selectNOIOrg_do_frm_organization="(.+?)" /> you could make these changes:

id" should be id=", organization=" should be organization{ and you can remove />

You might keep (.+?) but you could also use a negated character class to prevent unnecessary backtracking.

You could use match {, then use a capturing group and match what is inside using a negated character class ([^{}\n]+) and then match } again:

id="selectNOIOrg_do_frm_organization{([^{}\n]+)}"

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70