Regular Expressions (RE)

General Add comments
by:

One of the powerful tools introduced in the first Unix operating systems is regular expressions.

Regular expressions is a method for finding text not as an exact match but as a rule that applies to a text. i.e. ‘12aa12’ would apply to the rule: text starting with a 1. ( ^[0-9]*) .

    or

to the rule: starting with two numbers ([0-9][0-9].*) or 2 a’s between numbers ([0-9]+aa[0-9]+).

With regular expressions you replace every line that starts with 2 spaces into a TAB or find a text like “ERROR” or “error” or “Errors” or “eRRorS” in one run.
Regular expressions trace back to the work of an American mathematician by the name of Stephen Kleene (one of the most influential figures in the development of theoretical computer science) who developed regular expressions as a notation for describing what he called “the algebra of regular sets.” His work eventually found its way into some early efforts with computational search algorithms, and from there to some of the earliest text-manipulation tools on the Unix platform (including ed and grep). In the context of computer searches, the “*” is formally known as a “Kleene star.” (introduced in the 1950s.)
Ken Thompson made regular expressions available in Unix tooles like vi, grep, ed, sed.

Ok, done now with this history lesson Oh Really?

Javascript supports regular expressions and that makes it available to ServiceNow.
So let’s look a bit closer to things you can use in your ServiceNow Javascripts….

Javascript syntax:

var patt=new RegExp(pattern,modifiers);

or more simply:

var patt=/pattern/modifiers;

Patterns are like:

[abc] Find any characters between the brackets
[^abc] Find any characters not between the brackets
[0-9] Find any digit from 0 to 9
[A-Z] Find any characters from uppercase A to uppercase Z
[a-z] Find any characters from lowercase a to lowercase z
[A-z] Find any characters from uppercase A to lowercase z
[adgk]  Find any character in the given set
 [^adgk] Find any character outside the given set
 (red|blue|green) Find any of the alternatives specified

Modifiers

Modifiers are:
i for case insensitive
g for global matching (do not stop after the first match)
m for multiline matching

There are quite some metacharacter one can use, but for the next example I give you one:
Find a whitespace character \s (=space, tab’s)

Here is an example used to prevent users to add heading and leading spaces on configuration item names which can lead to double entries and/or errors on reporting.
[cc lang=”javascript”]
function checkTrailingSpaces()
{
// test any leading trailing spaces
var pm = current.name;
var re = /^\s+|\s+$/g;   
 if(pm.match(re))
{
gs.addErrorMessage(“Record is NOT saved!! CI Name contains trailing spaces. Please check the CI name for any spaces or tabs at the beginning and/or the end of the CI name”);
current.setAbortAction(true);
}
}
[/cc]
This function tests on one or more (+) spaces and tabs (\s) at the beginning of a line (^) or any tabs/spaces at the end of a line ($) .
If so the error message is shown and the running action aborted.

Links

Using the links below you can find more information on regular expressions:
http://www.regular-expressions.info/tutorial.html
http://en.wikipedia.org/wiki/Regular_expression
http://www.regular-expressions.info/javascript.html

If you have any questions you can contact me on .img[at].img.

2 Responses to “Regular Expressions (RE)”

  1. Sam Polk Says:

    SNC.Regex exposes Java’s Pattern and Matcher classes to JavaScript and it is pretty efficient but not documented.

    var strng = “!@123abcdefghijkl mnopqrstuvwxyzABCDEFGHIJKLMNOPQR STUVWXYZ4567890!@#$%^&*()_+{}|:\\\/.?”

    var nwStrng = “|”;

    var pttrn = “/\\w?+/”;

    //pttrn = “/[a-z]/i”;

    //pttrn = “/\\d{1,3}+/”;

    var rgx = new SNC.Regex(pttrn);

    var rplc = rgx.replaceAll(strng,nwStrng);

    var mtch = rgx.match(strng);

    var twStrng = rgx.toString();

    gs.print(rplc);

    gs.print(mtch);

    gs.print(twStrng);

    Sam

  2. Sam Polk Says:

    More Information on SNC.Regex

    http://wiki.servicenow.com/index.php?title=SNC_Regex_API

    https://hi.service-now.com/kb_view.do?sysparm_article=KB0540100

Leave a Reply