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.


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.


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.

Javascript Regular Expression – Linux Hint
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.

Javascript Regular Expression – Linux Hint
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) ;

Javascript Regular Expression – Linux Hint
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) ;

Javascript Regular Expression – Linux Hint
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) ;

Javascript Regular Expression – Linux Hint
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) ;

Javascript Regular Expression – Linux Hint
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) ;

Javascript Regular Expression – Linux Hint
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) ;

Javascript Regular Expression – Linux Hint
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) ;

Javascript Regular Expression – Linux Hint
Хорошо! 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.

Javascript Regular Expression – Linux Hint
Good so far. For example, you want to change the semicolons in the next printout:

Javascript Regular Expression – Linux Hint

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

Javascript Regular Expression – Linux Hint
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) ;

Javascript Regular Expression – Linux Hint
You can even enter a series of letters like this

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

Javascript Regular Expression – Linux Hint
Oder, Zahlenreihe:

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

Javascript Regular Expression – Linux Hint
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) ;

Javascript Regular Expression – Linux Hint
This function is useful for receiving information from users and for testing and verifying this information, including checking e-mail, telephone or date.


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

PTA conditionally unblocks TikTok in Pakistan – Tech Prolonged

The Pakistani Telecommunications Authority (PTA) has decided to release TikTok ten days…

4 ways to select columns from a dataframe with dplyr’s select()

4 ways to select columns with dplyr select() The dplyr select() function…

Top 5 Best apps like OfferUp (2020)

OfferUp and similar applications/websites allow users to sell products online. Unlike Amazon,…

Egor Melnikov, Founder And CEO OF ARNA Genomics

Bookmark(0) Log in to create bookmarks No bill yet? Login Egor Melnikov…