Table Of Contents
Before we get started with learning some basic regex methods let’s first understand its syntax, which looks something like this:
for example, if we have:
then we will be doing a global search for the pattern ’thenetblog' easy enough right?
Regexp Search Method
As I mentioned before, regexp is commonly used to both search, and replace a pattern of text.
search() method will return the position of the match (if found), else it will return
let searchThis = "the best blog is The Net Blog fr" let pattern = searchThis.search(/The Net Blog/i) // expected return: 17 console.log(pattern) // tip: console log is your friend when testing!
Good so far? Great, cause this next part is not that hard either!
Regexp Replace method
replace() method will replace the indicated pattern with a string of text.
text.replace(/pattern/modifiers, "replaced text")
let replaceThis = "The Quick Brown Fox Jumps Over The Lazy Dog" let pattern = replaceThis.replace(/Dog/i, "Cat") console.log(pattern) // should return: "The Quick Brown Fox Jumps Over The Lazy Cat"
RegExp Modifiers & Patterns
Patterns and Modifiers can help us perform more complicated searches, and can help us to find some special characters that can only be searched with patterns.
These are some of the more basic and common modifiers
|i||Preforms Case-insensitive search|
|g||Preforms Global Search|
Patterns can easily be identified as they are commonly placed between brackets or braces.
|[xyz]||Searches for the characters placed inside the brackets|
|[0-9]||Searches for the digits placed inside the brackets|
Another important one is the
(x|y|z) which searches for alternatives separated with the
(Couldn’t include in the table as markdown uses the same character to create tables.)
I appreciate you spending your time reading this post, if you’d like to read more, here you go: