Basic Regular Expressions In Javascript
23 February 2022

Regular expressions are patterns or sequences of characters used to match character combinations in strings which can as well form search patterns and serve as text replacement operations.
They exist in almost all the programming languages and they have similar syntax in all, they can be constructed in two methods either by using a regular expression literal of words between slashes or call the constructor function of the RegExp object.
See demonstrations and instances below.
Flags
There are various literal characters & meta character symbols in regular expressions used to set rules or restrictions for declared expressions, using the first method of constructing regular expressions for demonstrations below:
The case insensitive flag
This flag is denoted with the letter i to ensure the case of the declared regular expression is disregarded when executed and it is inserted at the end of the regular expression when used. Denotation: const disregardCase = /match/i.
The must start with flag
This flag is denoted with the caret ^ symbol and it ensures the passed statement when valuating a declared regular expression must start with the declared regular expression. Denotation: const mustStartWith= /^match/.
The must end with flag
This flag is denoted with the dollar $ sign and it ensures the passed statement when valuating a declared regular expression must end with the declared regular expression. Denotation: const mustEndWith= /match$/.
The skip character flag
This flag is denoted with the bullet . symbol and it implies “skip” for a single character, for instance when declaring a regular expression and then input this flag in between the regular expression when valuating the declared regular expression it would allow any single character passed in place of the flag. See code demonstration below.

The global flag
This flag is denoted with the letter g to ensure the declared regular expression is tracked at every instance in the passed statement when valuated, just as used in the skip character flag demonstration above. Denotation : const trackAtEveryInstance = /match/g.
The wild skip character flag
This flag is denoted with the asterisk * symbol and it implies “skip” for multiple characters, for instance when declaring a regular expression and then input this flag in between the regular expression when valuating the declared regular expression it would allow any characters passed in place of the flag. See code demonstration below.

The optional flag
This flag is denoted with the question mark ? symbol to set optional character cases when declaring the regular expression.
Denotation : const optionalCharacterCase = /advic?s?e/.
This would allow either advice or advise as a passed statement due to the optional flag for either letter c or s when valuating, but if we pass advie it would still be allowed because c and s are optional.
The escape flag
This flag is denoted with the backward slash \ symbol to ensure the next character is regarded as a literal and not a flag.
Denotation : const escapedCharacterCase = /advice\?/.
This would pass the declared regular expression as advice? a normal literal while disregarding the question mark as a flag.
The one or more flag
This flag is denoted with the addition + symbol which implies the next character and more in the declared regular expression.
Denotation : const escapedCharacterCase = /share this article+/.
Ways used to valuate regular expressions
The ".source" method
Using the first method of constructing the regular expression we can valuate any statement at face value in the expression using the .source object method, this would disregard the slashes and return the actual expression statement. See demonstration below.

The function methods
We can use specific functions to valuate regular expressions as well and the first one is:
The match() function
This function sorts out a passed statement for the declared regular expression then returns the expression in an array if the statement contains a match, if not it would return a null statement. See demonstration below.

and we can get various array attributes just like every other array.
The exec() function
This function sorts out a passed statement for the declared regular expression then returns the expression in an array if the statement contains a match, if not it would return a null just like the match() function but has a different approach. See demonstration below.
The test() function
This returns a true or false if there is a match in the passed statement and the declared regular expression just like a boolean case. See code representation below.
you guessed right this would return a false due to the absence of the first word of the declared regular expression in the passed statement.
The search() function
This would return the index of the first match of the declared regular expression in the passed statement and when not found would return a -1 response just like the indexOf method. See code representation below.
The replace() function
This does exactly as it implies it replaces a statement from the declared expression with the other matching statements of the expression.
See code representation below.
Character Sets and Quantifiers
Character Sets
These are optional characters in a bracket [ ] set in a declared regular expression.
Denotation : const optionalCharacterCaseSet = /advi[sc]e/.
This method is advised in place of the optional flag and its better because it makes sure at least one of the characters in the set is passed if not it would return null.
We can as well make use of flags in the character sets but in most cases would change the the flags function, For instance if we insert the “must start with” ^ flag it’s function would change to exemption, which implies any character would be accepted except for the characters in the case.
The character set can also accept a range of characters either alphabets or numbers. See instances below.const alphabetCharacterRangeSet = /advi[a-z]e/i.const numberCharacterRangeSet = /advi[1-9]e/.
Quantifiers
These are digits in curly braces { } in a declared regular expression used to quantify the quantity of the character before it in the expression when valuating.
Denotation : const quantifiedCharacterCase = /subscribe and share this article!{6}/.
This would ensure the exclamation mark is six in number else would return a null.
And just like character sets we can range the quantities using the quantifiers.
Denotation : const quantifiedCharacterCase = /subscribe and share this article!{3,6}/.
This would make sure the quantity of the exclamation mark is between the range of 3 and 6.
Also, we can issue an “at least” condition using the quantifiers denoted with a comma after the conditioned number. See demonstration below.const quantifiedCharacterCase = /subscribe and share this article!{3,}/. This would ensure the exclamation mark occurs at least three times or more.
Regex shorthand Characters
These are abbreviations and symbol classes in regular expressions used for short and rapid declarations. To mention a few we have:
/\w/ — This means that the passing character can either be a word, underscore _ , or an alphanumeric character and it’s denoted with a backward slash \ and a lowercase “w”.
/\W/ — This means that the passing character can neither be a word, underscore _ , nor a numeric character and it’s denoted with a backward slash \ and an uppercase “W”.
/\d/ — This means that the passing character can only be a numeric character and it’s denoted with a backward slash \ and a lowercase “d”.
/\D/ — This means that the passing character can not be a numeric character and it’s denoted with a backward slash \ and an uppercase “D”.
/\s/ — This means that the passing character has whitespace in place of the shorthand character when being evaluated and it’s denoted with a backward slash \ and a lowercase “s”.
/\S/ — This means that the passing character must not have whitespace in place of the shorthand character when being evaluated and it’s denoted with a backward slash \ and an uppercase “S”.
There are more regex shorthand characters, theses are just a few essential instances, learn about regex extensively here and here
Conclusion
Multiple character sets are allowed but not advised in a single declared regular explanation.
We can group regular expressions using parenthesis.
Do ask questions for clarifications and make corrections & suggestions, I expect them.
Also published on Medium.