JS Regex Element Find

Final latest stable v42

by dixalex

HTML

<pre>
<?xml version="1.0" encoding="UTF-8"?>
<DataDictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="pfdictionary.xsd">
  <Description>–%%%%%%%%%%%%%130609 & -++ %% Cracker Craze––+/()</Description>
  <Value code="1">Triscuit Lapsed Buyers</Value>
  <Survey>
    <StartDate>2023-06-29</StartDate>
    <EndDate>2023-07-20</EndDate>
    <Projectable>Yes</Projectable>
  </Survey>
  <FieldList>
    <Field name="hhid" col="1" len="8" range="true">
      <HHID>
        <Range min="00000001" max="99999999" />
      </HHID>
    </Field>
    <Field name="Q4_1" col="10" len="6">
      <Description>SURVEYNUMBER (SURVEYNUMBER)</Description>
      <String />
    </Field>
    <Field name="Q50_1" col="16" len="15">
      <Description>QMARSCNAME (QMARSCNAME)</Description>
      <String />
    </Field>
    <Field name="Q51_1" col="31" len="1">
      <Description>Buyer Group</Description>
      <String>
        <Value code="1">Triscuit Lapsed Buyers</Value>
      </String>
    </Field>
    <Field name="Q51_2" col="32" len="1">
      <Description>Buyer Group</Description>
      <String>
        <Value code="1">Triscuit Retained Buyers</Value>
      </String>
    </Field>
    <Field name="Q51_3" col="33" len="1">
      <Description>Buyer Group</Description>
      <String>
        <Value code="1">Triscuit Light Buyers</Value>
      </String>
    </Field>
    <Field name="Q52_1" col="34" len="1">
      <Description>Rep Sample</Description>
      <String>
        <Value code="1">Rep</Value>
      </String>
    </Field>
    <Field name="Q25" col="35" len="1">
      <Description>Have you purchased Crackers in the last 12 months?</Description>
      <String>
        <Value code="1">Yes</Value>
        <Value code="2">No</Value>
        <Value code="3">Don’t know</Value>
      </String>
    </Field>
    <Field name="Q2_A_1" col="36" len="1">
      <Description>Please indicate how important each of the following is to you when deciding what...

JavaScript 1.7

//https://stackoverflow.com/q/2021053/5076162
//var pattern = /<Description>([^<]*)<\/Description>/gi;
var pattern = new RegExp('<(Description|Value)(\\b[^>]*)>([^\<]*)<\/\\1>', 'gi'); 
//Leverage jQuery to read deliberate pre element wrapper that contains raw XML
var XMLString = $('pre').html().toString();

XMLString = XMLString.replace(pattern, function (match, tag, attrs, textContent, matchIndex, originalString) {
var invalidChars = "[\-]~[\’]*~[\“]*~[\”]*~[\‘]*~[\–]*~[\+]*~[\%]*~[\&]*~[\(]*~[\)]*~[\/]*~[\]*".split('~');

if(matchIndex > 0) {

textContent = textContent.replace(/(–)|(-)|(‘)|(’)|(“)|(”)|(\+)|(%)|(\&amp;)|(\()|(\))|(\/)|(\\)/g, function(replaceStasis) { 
    return {
    				'-':'HYPHEN',
            '–':'LONG.HYPHEN',
            '‘':'SMART.QUOTE.A',
            '’':'SMART.QUOTE.B',
            '“':'D.SMART.QUOTE.A',
            '”':'D.SMART.QUOTE.B',
            '+':'PLUS',
            '%':'PERCENT',
            '&':'AND',
            '(':'OPEN.PARENTHESIS',
            ')':'CLOSE.PARENTHESIS',
            '/':'FORWARD.SLASH',
            '\\':'BACK.SLASH',
            '\&amp;':'',
            '\&':''
           }[replaceStasis]; 
});

} //Match found conditional

if (attrs.length > 0) {
return "<" + tag + attrs + ">" + textContent + "</" + tag + ">";
} else {
return "<" + tag + ">" + textContent + "</" + tag + ">";
}
});

console.log(XMLString);
$('pre').html($.parseHTML(XMLString));

/*
https://stackoverflow.com/questions/77623171/java-xml-node-regex-string-manipulation-return-node-content-variably-excludin

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HelloWorld {

    public static void main(String []args) {
        String xml = "<Description>–%%%%%%%%%%%%%130609 & -++ %% Cracker Craze––+/()</Description><Value code='1'>Triscuit Lapsed --Buyers</Value>";
        //String xml = "<Description>–%%%%%%%%%%%%%130609 & -++ %% Cracker Craze––+/()</Description><Value>Triscuit Lapsed --Buyers</Value>";

        //...