Event Rule - Regular Expression to remove a certain part of the String

Ashok10
Mega Expert

Hi There,

I am trying to write a regular expression in the event rule Transform section. I am getting extra characters in FQDN and i am trying to remove them.

The example strings are :

abcd-asm.vdcunix.local

abcdasm.vdcunix.local

i am trying to remove asm and -asm from the string. I used (\|*-asm|asm\|*) but it is returning as Unknown . 

Expected outputs is:  abcd.vdcunix.local

Kindly help me with expression here.

14 REPLIES 14

Noah Drew
ServiceNow Employee
ServiceNow Employee

Hello Ashok!

Try the following:

var originalString = 'abcd-asm.vdcunix.local';
var goodString = originalString.replace(/asm|-asm/, '');
gs.print(goodString);

You can change the first string variable to different strings to confirm it works.

Try running it in System Definition > Scripts - Background

Hope that helps!

If it did, please mark as Helpful and consider setting the reply as the Correct Answer to the question, thanks!

Hey Noah,

Thank you for your reply. I am writing this regular expression in Event rule. PFA screenshot.

I dont think i can write code in event rule.Please advice

patrickkenney
Kilo Expert

I don't believe event rules allow for coding. This poses a problem as complex if then or is not allowed.

 

I have found that I need to glob multiple parts then reassemble using the variables pulled from the regex. Examples:

(.*)asm(.*)

and

(.*)-asm(.*)

Name the variables for the data globbed the params and then combine them back in the field of your choosing.

Example

$var1$var2

 

You will need two rules to remove the "-asm" and the "asm". The filter for the first rule would need to have the search for "-asm" at a lower sort order so that it is tried first. If the field you are filtering on does not have "-asm" in it then it would be picked up by the next filter using just "asm"

 

The rules would be identical wit hthe exception of one filtering on a match of "-asm" and one filtering on a match of just asm. This is to allow the regex to work for each format.

Thanks Patrick this helps alot. Do you think is there any way to remove the asm and-asm by writing expression?