Regular expressions can be created in 2 ways.
- new regEx()
Regular expressions using literal expressions
var data = “Trump hits record”;
var regExp = /([abcde])/g
while (match = regExp.exec(data))
console.log(“Match “, match, “found at”, match.index);
We can have below types of modifiers.
- “g” modifier is used when you want to match the pattern at global level (all matches).
- “i” modifier is used if you want to match the pattern in case insensitive manner.
- “m” modifier is used when you want to match the pattern on multiple lines.
Regular expression object has below methods.
- test() – This method returns true if the pattern is found in the string, otherwise it returns false.
- exec() – This method returns the first match in the string. If match is not found, it returns null.
Regular expressions using RegExp constructor
When you use constructor, you will have to escape “\” character.