{"id":3027,"date":"2019-06-12T00:46:25","date_gmt":"2019-06-12T00:46:25","guid":{"rendered":"http:\/\/softlect.in\/?p=3027"},"modified":"2019-06-15T02:39:49","modified_gmt":"2019-06-15T02:39:49","slug":"javascript-regular-expressions","status":"publish","type":"post","link":"http:\/\/softlect.com\/index.php\/javascript-regular-expressions\/","title":{"rendered":"JavaScript &#8211; Regular Expressions"},"content":{"rendered":"<p><strong>Regular Expression<\/strong><\/p>\n<p>The regular expressions are used to form patterns that can be matched against strings. For example, suppose a text string needs to be validated to check whether its an email addresses, for this string object does not provide any method except to compare strings which is not accurate and to overcome this problem we use regular expressions. To create regular expressions we must use RegExp literal. To do this regular expression is assigned to a variable using forward slash ( \/ ) instead of quotations. For example:<\/p>\n<p>var var-name = \/pattern\/flags;<\/p>\n<p>Here pattern is the regular expression and flag is optional.<\/p>\n<p>match() method compares a string or regular expression to see whether they match<\/p>\n<p>replace() method search the regular expression and then replace it with new string.<\/p>\n<p>search() method searches the match between regular expression and specified string.<\/p>\n<p>&nbsp;<\/p>\n<p>The pattern matching can be performed using RegExp object and String object. For example:<\/p>\n<p>var str = \u201cabcxyz\u201d;<\/p>\n<p>var position = str.search(\/cx\/);<\/p>\n<p>In the above code str variable is declared and position is searched for regular expression \u201c\/cx\/\u201d. The variable position will have the result 2. The search method returns the number found at position matching the regular expression or -1 if not found.<\/p>\n<p>&nbsp;<\/p>\n<p>The regular expression consists of normal characters and meta characters. The meta characters used are:<\/p>\n<p>\\\u00a0 |\u00a0 (\u00a0 )\u00a0 [\u00a0 ]\u00a0 {\u00a0 }\u00a0 ^\u00a0 $\u00a0 *\u00a0 +\u00a0 ?\u00a0 .<\/p>\n<p>&nbsp;<\/p>\n<p>The meta character \u201c.\u201d period is used to match any character except newline. For example:<\/p>\n<p>\/prog.\/<\/p>\n<p>will match progr, progs, proga but will not match aprogram.<\/p>\n<p>&nbsp;<\/p>\n<p>The meta character \u201c\\\u201d backslash is used as escape sequence. For example:<\/p>\n<p>\/3.4\/ will match 374, 304, 314<\/p>\n<p>\/3\\.4\/ will match 3.4<\/p>\n<p>&nbsp;<\/p>\n<p>The meta characters \u201c[ ]\u201d is used to match class of characters. For example:<\/p>\n<p>[abc] will match the a, b or c characters.<\/p>\n<p>[a-h] will match any lower case characters from a to h<\/p>\n<p>&nbsp;<\/p>\n<p>The meta character \u201c^\u201d is used as negation. For example:<\/p>\n<p>[^abc] will match any character except a, b or c; (Inverts the set)<\/p>\n<p>&nbsp;<\/p>\n<p>There are some predefined character classes:<\/p>\n<p>\\d\u00a0\u00a0\u00a0\u00a0\u00a0 [0-9] a digit<\/p>\n<p>\\D\u00a0\u00a0\u00a0\u00a0 [^0-9] not a digit<\/p>\n<p>\\w\u00a0\u00a0\u00a0\u00a0 [A-Z a-z 0-9] Alphanumeric characters<\/p>\n<p>\\s\u00a0\u00a0\u00a0\u00a0\u00a0 [\\r\\t\\n\\f ] white space characters<\/p>\n<p>\\S\u00a0\u00a0\u00a0\u00a0 [^\\r\\t\\n\\f ] Not a white space characters<\/p>\n<p>&nbsp;<\/p>\n<p>For example:<\/p>\n<p>\/\\d\\d\\d\/\u00a0 matches three digits<\/p>\n<p>\/\\d\\.\\d\\d\/ matches digit followed by \u201c.\u201d Followed by 2 digits<\/p>\n<p>\/\\w\\w\\w\\w\/ matches any alphanumeric 4 character string<\/p>\n<p>&nbsp;<\/p>\n<p>The meta characters \u201c{\u00a0 }\u201d is used to match repeated part of pattern. For example:<\/p>\n<p>\/xy{4}z\/ matches xyyyyz<\/p>\n<p>&nbsp;<\/p>\n<p>There are three symbolic quantifiers:<\/p>\n<p>*\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 used to match zero or more repetitions.<\/p>\n<p>+\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 used to match one or more repetitions.<\/p>\n<p>?\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 used to match one or none repetitions.<\/p>\n<p>&nbsp;<\/p>\n<p>For example:<\/p>\n<p>\/\\d+\\.\\d*\/ \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 one or more digits followed by period followed by possibly more digits<\/p>\n<p>\/[A-Z a-z]\\w*] \u00a0\u00a0\u00a0\u00a0\u00a0 a letter followed by zero or more alphanumeric characters.<\/p>\n<p>&nbsp;<\/p>\n<p>The meta character \u201c$\u201d is used to match the end of string. For example:<\/p>\n<p>\/cse$\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u201cThis is cse\u201d matches<\/p>\n<p>\u201cThis is cse department\u201d does not match<\/p>\n<p>&nbsp;<\/p>\n<p>More Examples:<\/p>\n<p>\/\\d{3}-\\d{4}$\/ \u00a0\u00a0\u00a0\u00a0 123-4567<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Regular Expression The regular expressions are used to form patterns that can be matched against strings. For example, suppose a text string needs to be validated to check whether its&hellip; <\/p>\n","protected":false},"author":1,"featured_media":3040,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[78],"tags":[],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts\/3027"}],"collection":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/comments?post=3027"}],"version-history":[{"count":3,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts\/3027\/revisions"}],"predecessor-version":[{"id":3436,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts\/3027\/revisions\/3436"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/media\/3040"}],"wp:attachment":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/media?parent=3027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/categories?post=3027"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/tags?post=3027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}