Help with REGEX to extract value from a multi-line text field

HugoFirst
Kilo Sage

I can't get a regex to extract a value from a multi-line text field, and I'm looking for advice to get it to work.

Here's my use case:   I have a catalog form   with a reference field where the user selects a service.   Once a service is selcted, the form should then obtain text from the corresponding service's record, extract a value and set it in another variable in the form.   The example used in these scripts is to find the string "cpus=" and return the value which follows ( a 2 in this case ).

Here's the conundrum: I have a short "demo" catalog client script which has a variable set to a multiline text value.   The regex works in this script.

I also   have a "real" catalog client script (C-C-S) which obtains the record and then attempts to parse   a multi-line text field to extract a value for it.   It always show a null as the extracted value.

I'm providing both forms of the C-C-S for your edification.

******************* First the "demo" script in which the regex "works": ****************

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

          return;

    }

  if (newValue != '' )

  {

  alert("debug 1.6");

  var spec = "These are the linux midservers used primarily to export attachments from the CMDB to the manifest area.\nOther services on these midservers are provided in the /servicenow file system.\nThe primary locations of services are:\n/servicenow/operational/software     for jobs which run in support of servicenow activities.\n/servicenow/operational/webapp/   for varfious web applications which support servicenow activities.\n\n# Service Config Info\n# Farm Name = none\n# Load balancer = none\n# DNS Name Pattern =   snmidsy[server#]ex[[datacenter]\n# Network Access = to all devices on all subnets\n# Data Center = ColumbusMetro-M1\n#Subnet=132.174.73\n#\n#Server Config\n#Type=Virtual Machine\n#cpus=2\n#Memory(GB)=4\n#CPU Speed(Ghz)=2.5\n#Filesystem=/servicenow\n#Filesystem=/Web\n#Local Disk Space(GB)=100\n#Operating System=Linux CentOS\n#Software=perl\n#Software=putty\n#Software=ServiceNow-MidServer\n#group: servicenow\n#sudoers=snmid\n";

// var regex = new SNC.Regex('/cpus=(.*$)/');

  alert("debug 10.1, ");

  var somevalue = spec.match(/cpus=(.*)$/m)[1]; // BINGO!

  alert("debug 11.1, somevalue=" + somevalue);

  }

  else

  {

  alert("debug-no action, newValue is null");

  }

}

*********************** end of demo script ***********************************

***********************   start of "real" script ***********************************

Now for the "real" script, where the regex returns null

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

          return;

    }

  alert("start 5.1");

  if (newValue != '' )

  {

              var rec = g_form.getReference('service');

              var spec = rec.u_description_common.toString();

  alert("11 spec=" + spec);   // shows that spec has the string as expected

              var somvalue = spec.match(/cpus=(.*)$/m)[1];   // somevalue is null

              alert('13 somevalue=', somevalue);

              g_form.setValue('server_spec', spec);

  }

}

*********************** end of "real" script ***********************************

4 REPLIES 4

philengles
Giga Expert

It seems like you are not declaring your regex. Also you could send back to the server with a script include if that wasn't the answer and do your parsing there; return what you want. Sorry for the trouble hope this helps!



Best,
    Philip E.


HugoFirst
Kilo Sage

Whoa, I posted too soon.   And I thank Philip for such a quick reply.


It turns our that the regex was indeed working.   But my alert command   to display the extracted value was coded incorrectly.


I wrongly coded it as this:


    alert('13 somevalue=', somevalue);



I corrected it by replacing the comma with a plus:


  alert('13 somevalue=' + somevalue);



So now I see the result and I am able to assign it to another form variable.


I apologize for not catching this before I posted.   I had iterated so many times over the different options for the regex, I never suspected the alert.


No matter how old I get, I'm still capable of making rookie mistakes! 🙂


I also want to flag this question as answered, but I don't see the red buttons to do so.   Another rookie mistake?


Haha glad to hear you got it and aren't we all. We will keep this one between us .