Deepa Srivastav
Kilo Sage

Hi Friends ,

It's my first blog and it may seem to be a small topic to write about but its surely an important concept in coding. I have seen quite a lot of posts (in community) and usage in Service Now .

I hope this blog helps you to understand about Regular expression

A regular expression is a special text/string for describing a search pattern. Use regular expressions to search through large numbers of text and binary files and then you can replace texts, rename files, copy files etc.

Meaning of different character/symbols:

  • Twelve characters have special meanings in regular expressions: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), the opening square bracket [, and the opening curly brace {. These special characters are often called "metacharacters". Most of them will produce are errors when used alone.
  • If you want to use any of these characters as a literal in a regex, you need to escape them with a backslash. So, if you want to match 2+3=5, the correct regex is 2\+3=5.

I would not mention all the concepts present in regular expression but will try to put all the important one's here.

  • Anchors do not match any characters. They match a position. ^ matches at the start of the string, and $ matches at the end of the string. Example:^l matches only first l in lol.
  • A character class [ ] specify a range of characters, We can use more than one range inside brackets. A caret after the opening square bracket negates the character class.

          Example: [0-9a-z] matches only 0-9 digits and alphabets in (smaller case).

  • Use curly braces { } to specify a repetition .Example: [A-Z]{2} two characters, each of which can be between A-Z.

Shorthand character classes:

  • \d matches a single character that is a digit.
  • \w matches a "word character" (alphanumeric characters plus underscore).
  • \s matches a whitespace character.
  • The dot matches a single character. Example: te.t matches text, test, tent etc.
  • |   is regular expression equivalent of "or".

To test if a particular regex matches (part of) a string, you can call the string match() or test() method.

Example:   if(regexp.test(someValue)){}

Note: Regex engines are case sensitive by default.


I found below link very useful for learning regex. It lets you write the pattern and shows whether its correct or wrong.

http://regexone.com/

Some community posts for similar topic which will help you understand more and solve your issue (if you have the same).

https://community.servicenow.com/thread/223016?start=0&tstart=0
https://community.servicenow.com/message/951790#951790
https://community.servicenow.com/message/930850#930850
https://community.servicenow.com/message/947083#947083
https://community.servicenow.com/thread/232272
https://community.servicenow.com/thread/232690

Hope you found this informative. Please encourage me to write more by hitting on like/share/bookmark (on top right corner)

10 Comments
swati38
Tera Expert

Thanks a lot Deepa. The blog was very helpful.


dirktromp
Tera Contributor

Thanks a lot Deepa. Also for the link to regexone. Great way to interactively learn regular expressiona.


Rajesh Mushke
Mega Sage

Hi   Deepa Srivastava.



it was awesome documentation,


thank you so much for sharing your experience on   regular express.


Deepa Srivastav
Kilo Sage

Thanks Swati, Dirk and Rajesh for your kind words


ramak
Giga Expert

Hi Deepa, thanks a lot for the info. I am actually stuck in a regex web now -



* I had to append a field value to a normal string. What I didn't see was that field was a reference field & after I executed the script, I saw the sysID of the ref field had been appended. So Immediately, I used getDisplayValue() and executed the script again...without thinking it through of course !!!



* Now the normal string after appending is looking like this : "School Admin- 3016666d0ffc56007038b57ce10 - AT017-Educational Services"



Is there any way of removing just the middle part of the above string so that it shows "School Admin- AT017-Educational Services"



Thanks


Deepa Srivastav
Kilo Sage

Is this the sys id? What you have written for getting it appended like this ...



Below can help you to get the digit extracted from the string and replace it with blank.



var numberPattern = /\d+/g;


var digt=current.fieldname.match(numberPattern);


var strfinal=current.fieldname.replace(digt[0],"");


Vladi1
Kilo Guru

Very helpful blog


LisaLatour
Administrator
ramak
Giga Expert

Thanks for the response. The sysIDs start with '-' and end with '-',so I need to remove only the alphanumeric characters between the 2 '-'.



var pattern = /^-[0-9a-zA-Z]{25-32}-$/; //search the pattern in the string starting with '-' and ending with '-' and the length between 25 & 32


var position = str.search(pattern);


var strFinal = replace(position[0], " ");



I tried this above script, but didn't work.


Manohararuna
Tera Contributor

Hello Deepa Srivastav,

 

         I have string variable in Incident form Call Number. as per requirement this string type field allowed only Numbers and symbol. i know how to write the expression where i will write it i don't know. Please tell me

 

Regards,

Manohararuna.