logo

NJP

Learn MORE JavaScript on the ServiceNow Platform: Lesson 6 - Regular Expressions (RegEx)

Import · Nov 27, 2023 · video

Hello Friends. In today's episode of Learn
more JavaScript on the ServiceNow platform,
we're going to unravel the complexities of regular expressions or RegEx for short. RegEx is a pretty powerful tool for
pattern matching and text manipulation. And once you get the hang of it, you'll be able to perform
some truly magical operations on strings. Let's get pattern matching. Okay, regular expressions are sequences of characters
that form a search pattern. They can be used for validating text. They can be used for performing replacements
or extracting information from strings. JavaScript has regex expressions
and objects built into its engine for you to use
in all of your scripting needs. So let's start creating a pattern. So let's say our word
pattern is the word “magic”. We want to look for the word magic
inside of another string. to create a regular expression,
you surround by slashes like this. There you go. I just created a
pattern for Reg expressions. Now let's see if we can try to find
that pattern inside of another string. So I'm going to go ahead and create
test string and I'm going to say the magic of RegEx starts here. So as we can see, we do see that
the word magic appears on there, and now we want to see if we can find it. So let's go ahead and do a console log and “does the word match?” And to do that test
where it's built into JavaScript, all you have to say is word pattern,
which is our regular expression variable, and then .test() for that string. So we're basically saying here using this pattern, the slash magic slash, let's test it on the string
and let's see what happens. True! It does. The .test() method does return true or
false, depending on if it's there or not? Let's kind of demonstrate
that even further. I'm going to break that string up
and then run that test again. And now it's false. So let's keep exploring. Regular expressions
aren't restricted to just letters. It's actually a lot more powerful
because you can use special characters and let me kind of demonstrate that
right now. So let's say we want to look for any numerical digits inside of a string. There's no button on my keyboard
that says any possible numerical digit. And so baked into regular expressions
are these special characters. And a lot of them
can be looked up on Google or any available reg expression websites and I'll include some of those in the description to help
you learn more regular expressions, but I'll be showing you
some of them right now. So to find any numerical digit you use a backslash and then press d. So this \d denotes
what I'm trying to look for. It basically says any digit. So let's go ahead and test that. So if I have a string with numbers in it, let's say “ServiceNow 2023”,
let's go ahead and do that test now. So I have a console log
and “is there a digit in here?” We're going to say digitPattern.test() And we're going to test it on our string
with numbers. Let's see what happens. True, there is a digit inside of that string and it found it
and now it's returning true. Let's use the same concept that we have. But let's say we're looking for
specifically a string that has four digits
right next to each other. To denote that in a regular expression
you use curly brackets and you tell it a number inside
that curly brackets to say how many of those characters
are you looking for? And so if I were to read this in RegEx
speak, this is basically saying, find me any string of four digits. That's what that's saying. And so if I press run script really quick,
let me match this really quick... “four digits?” and I run it. It's true. Now let's change it to only three digits. False. So it's specifically looking for strings
that are four digits long. Okay, let's let's kind of
put the pedal to the metal and get really complex because regex
allows for some really complex patterns. And if you look at this, I'm wondering
how many of you actually know what this is looking for
from the top of your head? If you if you did know,
let me know in the comments and I'll give you a thumbs up
for knowing what this is checking for from the top of your head, because I don't even know what that is
from the top of my head. I have to Google it whenever I look at...
when I see something like this. But what this is
doing, it's actually looking for emails. so to read this,
I'm actually going to go ahead and copy and paste it into one of my favorite regex
websites, regexr.com and I'm going to put it
inside of the expression. So there it is. And now I can read what it is here. So basically it's looking for
not a white space and it needs to have at least one
or more characters there. The actual @ symbol. And then again any non white space,
so any character, a digit or special characters in that case and it has to have more
than one of them a dot and then again not white space
and it has to have more than one of them. So if we test it here,
we can actually see that this isn't working. But if I put in my email address, it does create a match. And if I want to go barebones
and test it from the very minimum that it will need,
[email protected] is also something that works, but it's looking basically for letters
or some character before an @ symbol, then more characters, the dot
and then more characters. And that's that regex. I know emails
get a lot more complex than that, but for this case,
this is a pretty simplified regular expression to find email addresses
and to demonstrate it in ServiceNow I have these tests that I'm just going
to paste from the lesson examples and you will see that if we test it against this string,
it should return true. and if we test it against this string,
which is missing the dot it should return false. And there it goes. And one really quick method
I want to teach you about is .replace(), because it's a pretty powerful way
to manipulate your strings using regular expressions. So let's pretend that we have a pattern that's looking for the letters “SN”
next to each other. And then I'm going to add this “g” at
the end of it, because what that means is I want it to look globally
so not just the first instance of SN, but all the instances of this pattern
that might appear in a string. And let's get let's pretend
we have this string called “SN is awesome. SN rocks”, and then we're going to take
that and we're going to make a new string and it's going to be based off of the
replaceString, and we're going to replace all instances of that pattern with ServiceNow spelled out. So let's let's walk through this
really quick again. We have a pattern of looking for anything
that says SN next to each other. We have a string
that we want to manipulate and then we're going to create
a new string using this .replace() method. So we're going to take the string, we're going to .replace() it
because it's built into strings and we're going to give it
the pattern as a first parameter, and then we're going to say
what we want to replace it with: a string of ServiceNow. Let's see what happens. We're going to say log “replaced string”, and we're going to ask for the newString
and let's run it. There you go. It took the old string. We're only said SN
and replaced it with ServiceNow. So you can see why it's so useful
to know regular expressions, because you can make it very simple, like
a string replacement, a text replacement. But if you need to be much more complex
with: are there digits, are there white spaces,
are there new line characters which you can replace, you can find
or you can match them. You do all these different things
using regular expressions. Honestly, regex can have hours of content to teach you,
but these are some basics for you. I encourage you to dive into some regex resources or libraries or cheat sheets
is another way to look it up and honestly,
most people will just Google it when they need a regular expression
or a specific one because a lot of people
probably have already figured out
how to make those patterns for you. So take a look at it. Try that exercise next because it's
an interesting one and we'll see you soon.

View original source

https://www.youtube.com/watch?v=HydtEowB8YU