Regular expressions is a powerful way of doing search and replace in any strings, a regular expression describes a pattern of characters.
Patterns and flags
Slashes "/" indicate JavaScript that we are starting a regular expression,
just like quotes for strings.
var str = "WebTrainingRoom.Com is a eLearning Platform";
var wtrRegExp=/WebTrainingRoom/i
Modifiers
Modifier |
Description |
i |
Perform case-insensitive matching |
g |
Perform a global match (find all matches in given string rather than stopping after first match) |
m |
Perform multiline matching |
Brackets
Bracket is used for finding a range of characters:
Expression |
Description |
[xyz] |
Find any character between the brackets |
[^xyz] |
Find any character NOT between the brackets |
[0-9] |
Find any character between the brackets (any digit) |
[^0-9] |
Find any character NOT between the brackets (any non-digit) |
(a|b) |
Find any of the alternatives specified |
Methods of RegExp and String
str.search(reg)
str.match(reg)