matchAll() – It’s a String method that looks for all the matches in a string.If found returns the match if not, then returns null. match() – It’s a String method that looks for a match in a string.String methods that are useful when working with regular expressions. toString() – Returns an equivalent string object of the regular expression.That is, if a match is found, it returns true if not, then it returns false. test() – Tests for matches in a string and returns the result in the form of Boolean.If no match is found, then it returns null. exec() – Executes to look for matches in a string and returns the first match.Methods in Regular Expressions and StringsĬommonly used methods in regular expressions are as follows: Source – Gives the text of the regular expression pattern.Multiline – Checks whether the flag “m” has been set.The last Index – Determines the index position at which the next match needs to start from.Ignore case – Checks whether the flag “i” has been set.Global – Checks whether the flag “g” is set.Constructors – Determines which function has created the regular expression object prototype.Properties of Regular Expressions in JavaScript ?! – Matches any string that does not contain the regex pattern after the exclamatory sign.?= – Matches any string with the regex pattern after the equals sign.^ – Matches the given expression with the beginning of the string.$ – Matches the given expression with the end of the string.Matches the required regular expression the number of times mentioned in place of “n”. ? – Matches a string containing zero or one instance.+ – Matches a string containing one or more instances.
* – Matches a string containing zero or more instances.These characters are used along with regular expressions. Each special character has a meaning associated with it. These are denoted with the help of special characters. Quantifiers of Regular Expressions in JavaScript – All character from A to Z in upper case.– All characters from zero through nine.For example: means all characters from a through z in lower case will be taken into consideration. – Either a or b can be considered.Ī range is provided between that range all the characters are to be considered.– That is any character other than abc.Say, for the set – only the characters a, b and c are to be considered. In this scenario, square brackets are used as part: Sets and Ranges of Regular Expressions in JavaScript – A dot is used to find a single character other than the newline or end of the line. \W – Used to find anything except for words.\D – Used to find nun – numeric digits.\S – Used to find non-whitespace characters.\s – Used to find white space characters.\v – Used to find a vertical tab character.For example, “\n” denotes a new line.Ī few other examples of metacharacters are as follows: These are a combination of backward slash with an alphabetical character which together forms a metacharacter, and each of them has a special meaning associated with each of them. The metacharacters form the base of the regular expression pattern. Metacharacters of Regular Expressions in JavaScript The pattern consists of ranges, metacharacters, quantifiers, etc. Now, let us take a look at the patterns in the regular expression. I,you,we //prints the first word of every line. If it isn’t in multi-line mode, then only the matches from the entire string are returned. In the case of multi-line, it looks for matches at the start and end of each line rather than that of the entire string. It affects the behavior of characters “^” and “$”. If not set, then the search is case-sensitive.Ħ //returns the index at which the string is found.Īlert( str.search(/educba/) ) //without global flag This is case insensitive and matches both upper and lower cases. Scream, scream, scream // It returns all the matches in the string.
Var str = "I scream, you scream, we all scream for ice cream"
If not used, it stops after the first match.