Code Help in Regex

ST9
Tera Contributor

Hi All,

I wanted to extract a Order Number(123456789) and save that text to a particular custom field. if the order number is empty the, the custom field should be empty as well.

Example this- 

Order Number: 123456789
Candidate Name: Albert Test
Profile link: check

Hello

First Advantage is conducting a background check..

How do i achieve this using regex or any other way?

var des= current.description;
var index = des.indexOf("Order Number:");
var orderNum = des.substring(index+14, index+23);
	current.u_order_number= orderNum;

 

1 ACCEPTED SOLUTION

Hi @ST9 ,

You can modify the regular expression pattern and use the ^ and $ anchors.

Here's an updated example:

 

var des = current.description;
var orderNumRegex = /^Order Number:\s*([A-Za-z0-9]+)$/m;
var match = orderNumRegex.exec(des);
var orderNum = match ? match[1] : '';
current.u_order_number = orderNum;

 

In the updated code:

  • The ^ anchor at the beginning of the pattern ensures that the match starts at the beginning of a line.
  • The $ anchor at the end of the pattern ensures that the match ends at the end of a line.
  • The m flag is used after the closing / to enable multi-line mode so that ^ and $ match the beginning and end of each line in the input string.

 

If my response was helpful in resolving the issue, please consider accepting it as a solution by clicking on the Accept solution button and giving it a thumbs up 👍. This will benefit others who may have a similar question in the future.

 

Thank you!

Ratnakar

View solution in original post

11 REPLIES 11

Ratnakar7
Mega Sage
Mega Sage

Hi @ST9 ,

 

You can use regular expressions (regex). Here's an example of how you can achieve this in Javascript:

 

var des = current.description;
var orderNumRegex = /Order Number:\s*(\d+)/;
var match = orderNumRegex.exec(des);
var orderNum = match ? match[1] : '';
current.u_order_number = orderNum;

 

In the above code, we define a regex pattern /Order Number:\s*(\d+)/ which looks for the text "Order Number:" followed by any number of spaces (\s*) and then captures one or more digits ((\d+)). The parentheses around \d+ create a capturing group to extract the actual order number.

We then use the exec() method of the regex object to search for the pattern in the description string. If a match is found, match[1] will contain the captured order number. We assign this value to the u_order_number field of the current record (current.u_order_number).

If the order number is empty or not found in the description, we assign an empty string ('') to the custom field.

Make sure to adjust the code as per your specific field names and requirements.

 

If my response was helpful in resolving the issue, please consider accepting it as a solution by clicking on the Accept solution button and giving it a thumbs up 👍. This will benefit others who may have a similar question in the future.

 

Thank you!

Ratnakar

ST9
Tera Contributor

@Ratnakar7 ,
This was really helpful, thank you.
one last query what if the order number contains any Alphabets, then what will be changes?

Hi @ST9 ,

 

If the order number can contain both digits and alphabets, you can modify the regular expression pattern to include both. Here's an updated example:

 

var orderNumRegex = /Order Number:\s*([A-Za-z0-9]+)/;

 

In this updated code, the regex pattern /Order Number:\s*([A-Za-z0-9]+)/ includes the character range [A-Za-z0-9], which matches any uppercase letter (A-Z), lowercase letter (a-z), or digit (0-9). This allows the order number to contain both digits and alphabets.

 

Thanks,

Ratnakar

ST9
Tera Contributor

@Ratnakar7 ,

When i tried this in Background script where order number is empty -

var gr= new GlideRecord('sn_hr_core_case');
gr.addQuery('sys_id','4976d51d97b66510857c9904a253af41');
gr.query();
if (gr.next()) {
var des = gr.description;
var orderNumRegex = /Order Number:\s*([A-Za-z0-9]+)/;
var match = orderNumRegex.exec(des);
var orderNum = match ? match[1] : '';
gs.info(orderNum);
}

getting the o/p as- Candidate

ST9_0-1684154495396.png

it is picking up the next line of the description

ST9_1-1684154606650.png