If you use exec() or match() and if the match succeeds, these methods return an array and update properties of the associated regular expression object and also of the predefined regular expression object, RegExp. When you want to know whether a pattern is found in a string, use the test() or search() methods for more information (but slower execution) use the exec() or match() methods. Uses a regular expression or a fixed string to break a string into an array of substrings.
It returns the index of the match, or -1 if the search fails.Įxecutes a search for a match in a string, and replaces the matched substring with a replacement substring.Įxecutes a search for all matches in a string, and replaces the matched substrings with a replacement substring. Returns an iterator containing all of the matches, including capturing groups. Returns an array containing all of the matches, including capturing groups, or null if no match is found. It returns an array of information or null on a mismatch. MethodĮxecutes a search for a match in a string. Regular expressions are used with the RegExp methods test() and exec() and with the String methods match(), replace(), search(), and split(). If escape strings are not already part of your pattern you can add them using String.replace: a\*b/ and new RegExp("a\\*b") create the same expression, which searches for "a" followed by a literal "*" followed by "b". If using the RegExp constructor with a string literal, remember that the backslash is an escape in string literals, so to use it in the regular expression, you need to escape it at the string literal level. To match a literal backslash, you need to escape the backslash.įor instance, to match the string "C:\" where "C" can be any letter, you'd use /:\\/ - the first backslash escapes the one after it, so the expression searches for a single literal backslash.
Similarly, if you're writing a regular expression literal and need to match a slash ("/"), you need to escape that (otherwise, it terminates the pattern).įor instance, to search for the string "/example/" followed by one or more alphabetic characters, you'd use /\/example\/+/i-the backslashes before each slash make them literal. If you need to use any of the special characters literally (actually searching for a "*", for instance), you must escape it by putting a backslash in front of it.įor instance, to search for "a" followed by "*" followed by "b", you'd use /a\*b/ - the backslash "escapes" the "*", making it literal instead of special. If you want to look at all the special characters that can be used in regular expressions in a single table, see the following: Special characters in regular expressions. Unicode property escapesĭistinguish based on unicode character properties, for example, upper- and lower-case letters, math symbols, and punctuation. Indicate numbers of characters or expressions to match. Indicate groups and ranges of expression characters. For example, distinguishing between letters and digits. Character classesĭistinguish different types of characters. AssertionsĪssertions include boundaries, which indicate the beginnings and endings of lines and words, and other patterns indicating in some way that a match is possible (including look-ahead, look-behind, and conditional expressions). The following pages provide lists of the different special characters that fit into each category, along with descriptions and examples. In the string "cbbabbbbcdebc", this pattern will match the substring "abbbbc". So they are not equivalent.When the search for a match requires something more than a direct match, such as finding one or more b's, or finding white space, you can include special characters in the pattern.įor example, to match a single "a" followed by zero or more "b"s followed by "c", you'd use the pattern /ab*c/: the * after "b" means "0 or more occurrences of the preceding item." Option (C) will have 10 as substring but 0*(10*)* may or may not. Option (B) can generate 0100 but 0*(10*)* cannot. Option (A) can generate 101 but 0*(10*)* cannot. Solution : Two regular expressions are equivalent if languages generated by them are same. Question 3 : The regular expression 0*(10*)* denotes the same set as Option (D) says it can have any number of a’s and any numbers of b’s in any order.
But as shown in option (A), ba is also part of language. Option (C) says either it will have 0 or more a’s or 0 or more b’s or a’s followed by b’s. But But S -> bS => b is also a part of language. Option (B) says that it will have equal no. But S -> bS => baS => ba is also a part of language. Solution : Option (A) says that it will have 0 or more a followed by 0 or more b. Union : If L1 and If L2 are two regular languages, their union L1 ∪ L2 will also be regular.