Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

ST9
Tera Contributor

@Ratnakar7 , please help on the latest issue. 

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

ST9
Tera Contributor

Thanks @Ratnakar7 , this helped me:)

ST9
Tera Contributor

Hi @Ratnakar7 :

If the order number is in the format of like this WPS-9178990, then how to accommodate in regex..you code is for alphanumeric. how to capture the hyphen? 

Hi @ST9 ,

 

Please find below an updated example to capture the hyphen:

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;

 

Thanks,

Ratnakar