

{"id":65758,"date":"2019-08-09T15:18:03","date_gmt":"2019-08-09T09:48:03","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=65758"},"modified":"2021-02-17T21:32:17","modified_gmt":"2021-02-17T16:02:17","slug":"javascript-regular-expression","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/javascript-regular-expression\/","title":{"rendered":"JavaScript Regular Expression &#8211; How to Create &amp; Write them in JavaScript"},"content":{"rendered":"<p><em>The last tutorial on <a href=\"https:\/\/data-flair.training\/blogs\/javascript-characters\/\"><strong>JavaScript Characters<\/strong><\/a> was interesting, wasn\u2019t it?<\/em> The same concept extends to a little advance level in this tutorial. Here, in this article, we will implement everything that we have learned in our previous tutorial. It isn\u2019t that difficult, all you need to do is remember all the special characters, and their uses from our previous tutorial. We won\u2019t say you to learn them by heart with a single read, but at least you must understand the basics. Thus, you can\u2019t understand about JavaScript Regular Expression without having basic knowledge about JavaScript Characters.<\/p>\n<p>After knowing about JavaScript Characters, let\u2019s continue with our tutorial on JavaScript Regular Expression, we\u2019ll provide you with numerous real-life examples to lead you through the concept.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-66270\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression.png\" alt=\"How to write JavaScript Regular Expression\" width=\"803\" height=\"420\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression.png 803w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression-150x78.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression-300x157.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression-768x402.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression-520x272.png 520w\" sizes=\"auto, (max-width: 803px) 100vw, 803px\" \/><\/a><\/p>\n<h2>What is a JavaScript Regular Expression?<\/h2>\n<p>A JavaScript Regular Expression is an object, describing a pattern of characters in JavaScript. These help to match character combinations in a String. To make things easier for you, JavaScript also provides a regular expression object. Let\u2019s try to understand it with something we can relate to.<\/p>\n<p><em><strong>For Example <\/strong>&#8211;<\/em><\/p>\n<p><em>Suppose I have a text sentence, <strong>\u201cHello, this is DataFlair\u2019s JavaScript tutorial\u201d<\/strong>, and I want to search if it contains the word <strong>\u201cHello\u201d<\/strong>. How would I proceed? Easy, I\u2019ll search for an <strong>h<\/strong> followed by an <strong>e<\/strong>, then <strong>ll<\/strong> which is followed by a vowel <strong>o<\/strong> again. JavaScript computes things the same way: it searches for a pattern in the string<\/em>.<\/p>\n<p>Remember, a regular expression can be either a single character or a pattern of multiple characters. We can perform all sorts of text find and replace operations with their help.<\/p>\n<p><em><strong>You must go through our tutorial on <a href=\"https:\/\/data-flair.training\/blogs\/javascript-strings\/\">JavaScript Strings<\/a>, it will surely help you to make your understanding easier.<\/strong><\/em><\/p>\n<h3>How to Create JavaScript Regular Expression<\/h3>\n<p>There are two approaches to create a regular expression in JavaScript.<\/p>\n<h4>1. Using a regular expression literal<\/h4>\n<p>The script compiles regular expression literals when it loads in the memory. These improve the performance of a program marginally if the expression remains constant. A regular expression in JavaScript consists of a pattern wrapped with the slashes and the syntax is as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var re = \/regular expression\/;<\/pre>\n<h4>2. Using the constructor function<\/h4>\n<p>It provides runtime compilation of the regular expression and is an excellent way when the expression value changes regularly. Or it may be that you aren\u2019t sure of the pattern since you are retrieving it from an outside source, including user input. You can call the constructor function of the <strong>RegExp<\/strong> object with the help of the following syntax:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var re = new RegExp('regular expression');<\/pre>\n<h3>How to Write JavaScript Regular Expression<\/h3>\n<p>Remember, while going through our tutorial,\u00a0<em><strong><a href=\"https:\/\/data-flair.training\/blogs\/javascript-tutorial\/\">Introduction to JavaScript<\/a><\/strong><\/em>, we said that JavaScript provides the additional feature of email validation on the browser itself? Well, in this JavaScript tutorial, we will understand how we can perform it with the help of regular expressions. I want the user to enter an email address and JavaScript to perform validation. We will build up the concepts and at the end of this tutorial, will validate the email with a JavaScript code. The following are the basic components we need to work with regular expressions:<\/p>\n<h4>1. Using simple patterns<\/h4>\n<p>We use these patterns in case we want a direct match. Thus if I wanted to validate a password, this method is just what I need. The simple pattern will return true only if the characters are in the exact order and all the letters have the same case as required. But, this approach isn\u2019t suitable for my task of email validation. Well, no issues, JavaScript got something else for you to use in this situation: <strong>special characters<\/strong>.<\/p>\n<h4>2. Using special characters<\/h4>\n<p>You can include special characters (always the lifesavers) for searches that require more than a direct match. I know one thing that all emails have in common: a series of letters and the <strong>@<\/strong> sign followed by a series of letters, a dot and then again a set of letters. So what I need to do is search for this pattern and if the pattern holds in my string? Bam! I got a valid email address. If not, well, I just inform the user that the email entered is invalid. Okay, I\u2019m not talking gibberish, we will implement this concept later in the tutorial. We learned about all the special characters we need in our prior tutorial.<\/p>\n<h4>3. Escaping<\/h4>\n<p>Suppose you want to use a special character literally; what would you do now? That\u2019s right, we will use escape characters, starting with a <strong>backslash (\\)<\/strong>, to do that. We add a backslash before each special character to make them literals. For example, you can search for an expression <strong>a*b<\/strong> in your string with the help of the following code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var re = \/a\\*b\/<\/pre>\n<h4 class=\"EnlighterJSRAW\">4. Using parentheses<\/h4>\n<p>Parentheses around any JavaScript character or expression indicate that the content of the parentheses is a <strong>\u2018Capturing Group\u2019<\/strong>. This group stores the matched substring in a separate array. Also, if we add a quantifier after the parentheses, it applies to the entire parenthesis and not just the last character. The interpreter processes the contents of parentheses from left to right. We use non-capturing groups, parentheses with the preface <strong>?:<\/strong> to avoid remembering the matched substring.<\/p>\n<p>We will use parentheses in our program for email validation, but let\u2019s take a simple regular expression to understand the use of parentheses.<\/p>\n<p>In the example below, <strong>the + quantifier<\/strong> applies on the whole substring \u201cppi\u201d rather than just \u201ci\u201d.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var name = \"Mississippi\"\r\nname.match(\/(ppi+)\/)\r\n\/\/ (2) [\"ppi\", \"ppi\", index: 8, input: \"Mississippi\", groups: undefined]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression-using-parentheses.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"img-gray-border aligncenter wp-image-66230 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression-using-parentheses.jpg\" alt=\"JavaScript Regular Expression using parentheses\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression-using-parentheses.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression-using-parentheses-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression-using-parentheses-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression-using-parentheses-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression-using-parentheses-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression-using-parentheses-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><em><strong>Don&#8217;t forget to explore the article on <a href=\"https:\/\/data-flair.training\/blogs\/javascript-array\/\">JavaScript Array<\/a><\/strong><\/em><\/p>\n<h3>Working with JavaScript Regular Expressions<\/h3>\n<p>To work with regular expressions in JavaScript, we use the following <strong>RegExp<\/strong> and <strong>String<\/strong> methods:<\/p>\n<table dir=\"ltr\">\n<colgroup>\n<col width=\"107\" \/>\n<col width=\"99\" \/><\/colgroup>\n<tbody>\n<tr>\n<td style=\"text-align: center\"><strong>Method<\/strong><\/td>\n<td style=\"text-align: center\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center\">exec()<\/td>\n<td>\n<div>\n<div style=\"text-align: center\">It is a <strong>RegExp<\/strong> method, executes a search for a match in a string and returns an array of information or a NULL on mismatch.<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center\">test()<\/td>\n<td style=\"text-align: center\">\n<div>\n<div>It is a <strong>RegExp<\/strong> method, tests for a match in a string and returns true or false.<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center\">match()<\/td>\n<td>\n<div>\n<div style=\"text-align: center\">It is a <strong>String<\/strong> method that returns an array containing all the matches or NULL if no element matches.<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center\">matchAll()<\/td>\n<td style=\"text-align: center\">\n<div>\n<div>It is a <strong>String<\/strong> method returning an iterator containing all the matches.<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center\">search()<\/td>\n<td>\n<div>\n<div style=\"text-align: center\">It is a <strong>String<\/strong> method, tests for a match in the string and returns the index of the match if present, else <strong>-1.<\/strong><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center\">replace()<\/td>\n<td style=\"text-align: center\">\n<div>\n<div>This <strong>String<\/strong> method searches for a match in a string and replaces it with the replacement string.<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center\">split()<\/td>\n<td>\n<div>\n<div style=\"text-align: center\">This <strong>String<\/strong> method breaks a string into an array of substrings using a regular expression or a fixed string.<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Let\u2019s test these methods in our respective browser consoles. Define the following values first:<\/p>\n<ul>\n<li>var string = &#8220;This is DataFlair&#8217;s JavaScript tutorial&#8221;<\/li>\n<li>var myRe = \/a[a-z]a\/<\/li>\n<\/ul>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">myRe.exec(string)\r\n\/\/ [\"ata\", index: 9, input: \"This is DataFlair's JavaScript tutorial\", groups: undefined]\r\nmyRe.test(string)\r\n\/\/ true\r\nstring.match(myRe)\r\n\/\/ [\"ata\", index: 9, input: \"This is DataFlair's JavaScript tutorial\", groups: undefined]\r\nstring.matchAll(myRe)\r\n\/\/ RegExpStringIterator {}\r\nstring.search(myRe)\r\n\/\/ 9\r\nstring.split(myRe)\r\n\/\/ (3) [\"This is D\", \"Flair's J\", \"Script tutorial\"]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Working-with-JavaScript-Regular-Expressions.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"img-gray-border aligncenter wp-image-67513 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Working-with-JavaScript-Regular-Expressions.jpg\" alt=\"Working with JavaScript Regular Expressions - myRe.exec\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Working-with-JavaScript-Regular-Expressions.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Working-with-JavaScript-Regular-Expressions-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Working-with-JavaScript-Regular-Expressions-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Working-with-JavaScript-Regular-Expressions-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Working-with-JavaScript-Regular-Expressions-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Working-with-JavaScript-Regular-Expressions-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<h4>RegExp Properties<\/h4>\n<p>The most common properties of a RegExp object are as follows:<\/p>\n<table dir=\"ltr\">\n<colgroup>\n<col width=\"107\" \/>\n<col width=\"99\" \/><\/colgroup>\n<tbody>\n<tr>\n<td style=\"text-align: center\"><strong>Property<\/strong><\/td>\n<td style=\"text-align: center\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center\">global<\/td>\n<td style=\"text-align: center\">\n<div>\n<div>This property specifies if the \u201cg\u201d flag is set.<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center\">ignoreCase<\/td>\n<td>\n<div>\n<div style=\"text-align: center\">It specifies if the \u201ci\u201d flag is set.<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center\">lastIndex<\/td>\n<td>\n<div>\n<div style=\"text-align: center\">It indicates the index at which to start the next match.<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center\">multiline<\/td>\n<td>\n<div>\n<div style=\"text-align: center\">This property specifies if the \u201cm\u201d flag is set.<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Validating Emails with Regular Expressions<\/h3>\n<p>Finally, we\u2019ve come to the part where we start validating email addresses. Isn\u2019t it cool? I hope you have a basic understanding of what regular expressions in <a href=\"https:\/\/en.wikipedia.org\/wiki\/JavaScript\">JavaScript<\/a> are. Keep scanning the different characters associated with regular expressions and their functions in the beginning. Eventually, you won\u2019t need to turn back to the list for every character. As you practice, you\u2019ll be able to master them and use them without consulting. The code to validate email addresses using JavaScript is as follows:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;html&gt;\r\n  &lt;body&gt;\r\n\r\n    &lt;script&gt;\r\n      var email = prompt(\"Enter an email address\");\r\n      var myRe = \/^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$\/; \/\/ regular expression for validation\r\n      var checkEmail = myRe.exec(email); \/\/ check validity using myRe\r\n      if(checkEmail){\r\n        document.write(\"You entered the email: \" + email);\r\n      }else{\r\n        document.write(\"This is not a valid email address.\");\r\n      }\r\n\r\n    &lt;\/script&gt;\r\n\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p><strong>Screenshot:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Validating-emailwith-javascript-regular-expression.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-66231\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Validating-emailwith-javascript-regular-expression.jpg\" alt=\"Validating emailwith javascript regular expression\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Validating-emailwith-javascript-regular-expression.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Validating-emailwith-javascript-regular-expression-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Validating-emailwith-javascript-regular-expression-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Validating-emailwith-javascript-regular-expression-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Validating-emailwith-javascript-regular-expression-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/Validating-emailwith-javascript-regular-expression-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><strong>Prompt:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression-email-prompt1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"img-gray-border aligncenter wp-image-66232 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression-email-prompt1.jpg\" alt=\"JavaScript Regular Expression email prompt1\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression-email-prompt1.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression-email-prompt1-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression-email-prompt1-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression-email-prompt1-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression-email-prompt1-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression-email-prompt1-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><strong>Output (when I enter invalid email address):<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expressionemail-op1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"img-gray-border aligncenter wp-image-66233 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expressionemail-op1.jpg\" alt=\"JavaScript Regular Expressionemail op1\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expressionemail-op1.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expressionemail-op1-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expressionemail-op1-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expressionemail-op1-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expressionemail-op1-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expressionemail-op1-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><strong>Output (when I enter a valid email address):<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expressionemail-op2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"img-gray-border aligncenter wp-image-66234 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expressionemail-op2.jpg\" alt=\"JavaScript Regular Expressionemail op2\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expressionemail-op2.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expressionemail-op2-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expressionemail-op2-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expressionemail-op2-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expressionemail-op2-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expressionemail-op2-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<h2>Summary<\/h2>\n<p>Here we conclude our tutorial on JavaScript Regular Expression. In this article, we learned what regular expressions are and how to create and work with them in JavaScript. We also went through the various terminologies associated with regular expressions. Lastly, we created a JavaScript program using regular expressions to validate an email address.<\/p>\n<p><em><strong>Next, you must go through our article on <a href=\"https:\/\/data-flair.training\/blogs\/javascript-dataview\/\">JavaScript Dataview<\/a><\/strong><\/em><\/p>\n<p>Hope the information provided was fruitful to you. If you still have any queries, do share with us through the comment section.<span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:1454,&quot;href&quot;:&quot;https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/JavaScript&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20251208180004\\\/https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/JavaScript&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 07:25:01&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-12 10:12:19&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-19 20:12:26&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-23 14:26:43&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-26 20:33:05&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-30 03:46:49&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-03 10:04:10&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-08 07:59:48&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-11 13:08:47&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-16 08:13:22&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-20 13:51:36&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-24 03:23:30&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-28 01:26:04&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-31 06:28:41&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-03 10:56:46&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-10 05:25:16&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-13 14:40:22&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-16 17:27:49&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-20 11:10:49&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-26 10:59:31&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-02 23:12:45&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-07 09:23:37&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-16 15:08:09&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-20 11:51:02&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-26 05:27:11&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-01 15:53:39&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-05 04:38:58&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-10 05:37:32&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-14 10:47:54&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-17 11:57:53&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-21 02:42:11&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-24 09:17:36&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-28 02:46:05&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-01 07:17:08&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-05-05 18:21:06&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-11 11:12:51&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-14 16:04:50&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-17 16:56:01&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-21 17:58:16&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-25 08:21:54&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-30 04:19:24&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-02 22:21:06&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-06 18:08:05&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-11 08:17:02&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-15 08:00:22&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-15 08:00:22&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The last tutorial on JavaScript Characters was interesting, wasn\u2019t it? The same concept extends to a little advance level in this tutorial. Here, in this article, we will implement everything that we have learned&#46;&#46;&#46;<\/p>\n","protected":false},"author":7,"featured_media":66270,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18979],"tags":[20750,20839,20749,20751],"class_list":["post-65758","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-create-javascript-regular-expresion","tag-javascript-regexp-properties","tag-javascript-regular-expression","tag-work-with-javascript-expression"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JavaScript Regular Expression - How to Create &amp; Write them in JavaScript - DataFlair<\/title>\n<meta name=\"description\" content=\"Get all the concepts clear related to JavaScript Regular Expression. Learn how to create and write Regular Expression in JavaScript with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/javascript-regular-expression\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Regular Expression - How to Create &amp; Write them in JavaScript - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Get all the concepts clear related to JavaScript Regular Expression. Learn how to create and write Regular Expression in JavaScript with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/javascript-regular-expression\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-08-09T09:48:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-02-17T16:02:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression.png\" \/>\n\t<meta property=\"og:image:width\" content=\"803\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Regular Expression - How to Create &amp; Write them in JavaScript - DataFlair","description":"Get all the concepts clear related to JavaScript Regular Expression. Learn how to create and write Regular Expression in JavaScript with examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/javascript-regular-expression\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Regular Expression - How to Create &amp; Write them in JavaScript - DataFlair","og_description":"Get all the concepts clear related to JavaScript Regular Expression. Learn how to create and write Regular Expression in JavaScript with examples.","og_url":"https:\/\/data-flair.training\/blogs\/javascript-regular-expression\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2019-08-09T09:48:03+00:00","article_modified_time":"2021-02-17T16:02:17+00:00","og_image":[{"width":803,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression.png","type":"image\/png"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/javascript-regular-expression\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-regular-expression\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd"},"headline":"JavaScript Regular Expression &#8211; How to Create &amp; Write them in JavaScript","datePublished":"2019-08-09T09:48:03+00:00","dateModified":"2021-02-17T16:02:17+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-regular-expression\/"},"wordCount":1370,"commentCount":1,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-regular-expression\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression.png","keywords":["Create JavaScript Regular Expresion","JavaScript RegExp Properties","JavaScript Regular Expression","Work with JavaScript Expression"],"articleSection":["JavaScript Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/javascript-regular-expression\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/javascript-regular-expression\/","url":"https:\/\/data-flair.training\/blogs\/javascript-regular-expression\/","name":"JavaScript Regular Expression - How to Create &amp; Write them in JavaScript - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-regular-expression\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-regular-expression\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression.png","datePublished":"2019-08-09T09:48:03+00:00","dateModified":"2021-02-17T16:02:17+00:00","description":"Get all the concepts clear related to JavaScript Regular Expression. Learn how to create and write Regular Expression in JavaScript with examples.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-regular-expression\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/javascript-regular-expression\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/javascript-regular-expression\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Regular-Expression.png","width":803,"height":420,"caption":"How to write JavaScript Regular Expression"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/javascript-regular-expression\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"JavaScript Tutorial","item":"https:\/\/data-flair.training\/blogs\/category\/javascript\/"},{"@type":"ListItem","position":3,"name":"JavaScript Regular Expression &#8211; How to Create &amp; Write them in JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team specializes in creating clear, actionable content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Backed by industry expertise, we make learning easy and career-oriented for beginners and pros alike.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam3\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/65758","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=65758"}],"version-history":[{"count":9,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/65758\/revisions"}],"predecessor-version":[{"id":67694,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/65758\/revisions\/67694"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/66270"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=65758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=65758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=65758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}