A regular expression is an object in which the patterns are charted on the desired string.
The syntax of a regular expression is very simple and can be written as follows
A template is a string in which you provide a template that matches another string.
Flags are optional attributes used for different purposes. The G-flag, for example, means many others worldwide.
The scope of regular expressions is very broad. We show you the most important, the most necessary for programming, in a step-by-step explanation and with some real examples.
Let’s assume it’s a line first:
let str = Linuxhint works well. linuxhint works perfectly and works 100%.
We repeated the same big word and the Linux hint in the sentence. The purpose of this strange line will become clear in a moment.
As you can see, it indicates the index where this substrate comes from. We’ll continue now and try to do the same with the regex syntax.
Step 1: Searching for and replacing sub-channels
You can use a regular expression to search for a matching string by simply placing a background between two slashes in the expression.
As you can see, we had the same exit.
All right! Now let’s see what we can do with a common expression. Let’s try replacing the word cool with, say, cool with the replace() method.
str.replace (super, super) ;
Here you can see the problem: the first appearance is perfectly replaced and the second is not.
In the first level you just learned how to search for a line with a regular expression. We now move on to the next step and discover the concept of flags.
Step 2: Flags
If you want to replace all events perfectly, you can use a regular expression with a g-flag, which is the abbreviation for global.
str.replace(/big/g, super) ;
Perfect, all important things have now changed. But you may have a problem if you try to change all cases of, for example, linux hints on our site using the same technique.
We’ll try to do that first, and then we’ll see how we can solve the problem.
str.replace(/linuxhint/g, our website) ;
Although we have added a regular expression to the world flag, the first event does not change. It’s the sensitivity of the case. In this case, we must also set the insensitivity flag for registration i. You can do this by simply adding the i-flag to the g-flag.
str.replace(/linuxhint/gi, our website) ;
Great. As you can see, all cases in which the term linuxhint is used have changed on our site.
Alright! The job worked perfectly. The split() method provided a range of substrates based on the Linuxhint expression. But if you want to use separators and also in a series of sub strings, you have to play with the details.
That’s why in this phase we got to know the flags and how they help us. There are other flags. For example, m means a multi-line game, s means a point game, etc. We will now move on to the concept of models and learn how to use these elements.
Step 3: Models
At this stage you will learn how to use the templates and their options.
To include separators in the substring table, simply add brackets around the template as shown in the following figure :
Perfect! As you can see, the separators are also included in the table of substrings.
To separate the base of two separators, you can specify multiple substrates in a regular expression using the OR | operator.
Хорошо! The operator has done an excellent job, because we expect him to get a divorce.
Well, to separate the base from a space or a period, you need to add special characters to a regular expression by adding a backslash for all special characters.
Good so far. For example, you want to change the semicolons in the next printout:
Gaps are also used for other purposes. With w, d and s you can search for any word, number or space. For example, the following expression is used to replace spaces on the hyphenation mark
Awesome! Now you really see the potential of regular expressions.
Hooks [ ]
If you want to replace multiple characters in a string, you can put them all in square brackets and they will be replaced by a specific substring. For example, if you want to replace three letters in a string and don’t want to put many OR | operators in a regular expression, you can use a syntax with square brackets that allows you to specify multiple letters while doing so:
str.replace(/[nia]/g, u) ;
You can even enter a series of letters like this
str.replace(/[g-l]/g, u) ;
str.replace(/[0-5]/g, 9) ;
And if you want to exclude the characters between square brackets, you can use the car this way for example
str.replace(/[^g-l]/g, t) ;
This function is useful for receiving information from users and for testing and verifying this information, including checking e-mail, telephone or date.