Many programmers are familiar with the idea that a regular expression is a useful but underestimated concept. But they don’t really know how to use regular expressions effectively. Regular expressions are not only used in Javascript, but in almost all other programming languages. In this article you will discover the regular expressions step by step. The concepts discussed in this article should be easy to understand for programmers at all levels.

A regular expression is an object in which the patterns are charted on the desired string.

Syntax

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.

There are many methods for which you may need to use regular expressions, such as the Javascript search(), replace(), match() and split() methods. We’ll start with a simple search on the line without using regular expressions and show you later how to perform the same search with regular expressions.

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.

All right! Now just write the string search method in Javascript to find the term work.

http://31.220.61.170/wp-content/uploads/2020/10/Javascript-Regular-Expression---Linux-Hint.jpg--Linux-Hint.jpg
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.

http://31.220.61.170/wp-content/uploads/2020/10/1602980584_15_Javascript-Regular-Expression---Linux-Hint.jpg--Linux-Hint.jpg
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) ;

http://31.220.61.170/wp-content/uploads/2020/10/1602980584_856_Javascript-Regular-Expression---Linux-Hint.jpg--Linux-Hint.jpg
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

In this phase you will learn more about the concept and assignment of flags in regular expressions. We will use the Javascript replacement method to explain this idea.

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) ;

http://31.220.61.170/wp-content/uploads/2020/10/1602980584_858_Javascript-Regular-Expression---Linux-Hint.jpg--Linux-Hint.jpg
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) ;

http://31.220.61.170/wp-content/uploads/2020/10/1602980585_514_Javascript-Regular-Expression---Linux-Hint.jpg--Linux-Hint.jpg
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) ;

http://31.220.61.170/wp-content/uploads/2020/10/1602980585_774_Javascript-Regular-Expression-–-Linux-Hint.jpg
Great. As you can see, all cases in which the term linuxhint is used have changed on our site.

In the same way you can use regular expressions in the split() Javascript function.

str.split(/linuxhint/gi) ;

http://31.220.61.170/wp-content/uploads/2020/10/1602980585_734_Javascript-Regular-Expression-–-Linux-Hint.jpg
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 :

str.split(/(linuxhint)/gi) ;

http://31.220.61.170/wp-content/uploads/2020/10/1602980586_83_Javascript-Regular-Expression-–-Linux-Hint.jpg
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.

str.split(/linuxhint|great/gi) ;

http://31.220.61.170/wp-content/uploads/2020/10/1602980586_180_Javascript-Regular-Expression---Linux-Hint.jpg--Linux-Hint.jpg
Хорошо! The operator has done an excellent job, because we expect him to get a divorce.

Reverse slash

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.

http://31.220.61.170/wp-content/uploads/2020/10/1602980586_972_Javascript-Regular-Expression---Linux-Hint.jpg--Linux-Hint.jpg
Good so far. For example, you want to change the semicolons in the next printout:

http://31.220.61.170/wp-content/uploads/2020/10/1602980587_634_Javascript-Regular-Expression---Linux-Hint.jpg--Linux-Hint.jpg
Сработало!

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

http://31.220.61.170/wp-content/uploads/2020/10/1602980587_729_Javascript-Regular-Expression-–-Linux-Hint.jpg
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) ;

http://31.220.61.170/wp-content/uploads/2020/10/1602980587_551_Javascript-Regular-Expression---Linux-Hint.jpg--Linux-Hint.jpg
You can even enter a series of letters like this

str.replace(/[g-l]/g, u) ;

http://31.220.61.170/wp-content/uploads/2020/10/1602980588_900_Javascript-Regular-Expression---Linux-Hint.jpg--Linux-Hint.jpg
Oder, Zahlenreihe:

str.replace(/[0-5]/g, 9) ;

http://31.220.61.170/wp-content/uploads/2020/10/1602980589_220_Javascript-Regular-Expression---Linux-Hint.jpg--Linux-Hint.jpg
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) ;

http://31.220.61.170/wp-content/uploads/2020/10/1602980589_651_Javascript-Regular-Expression---Linux-Hint.jpg--Linux-Hint.jpg
This function is useful for receiving information from users and for testing and verifying this information, including checking e-mail, telephone or date.

Conclusion

This article has only scratched the surface of an ordinary Javascript expression. It only includes concepts from beginner to intermediate level. You can learn a lot more about regular expressions and you can do a lot of things you may not have thought of before. To further learn, work and get more experience with Javascript, check out the articles on this topic on linuxhint.com.awk command to find length of string,bash if string length greater than 0,bash repeat character n times,linux shell length,strlen function in unix,bash count characters in file,bash extract substring regex,bash string length,bash substring after character,bash string split,bash find substring,bash remove substring from middle of string,learn bash in,bash file extension,bash scripting and shell programming,bash operator,github bash scripts,bash pipe,write a shell script to find the length of a given string.,bash concatenate strings,bash if string length,bash substring,length of a string in bash script

You May Also Like

TrickBot Botnet Survives Takedown Attempt

The TrickBot seems to have resumed normal operation a few days after…

10 Best Online Survival Games For Android 2020

10 Best Online Survivor Games for Android – Looking for the best…

10 Best Free VPN Software for Windows and Mac in 2020

As you may already know, your Internet access or service provider normally…

Privacy and Security in Abusive Relationships

A few weeks ago, the technology news site The Verge reported a…