0% found this document useful (0 votes)
26 views2 pages

Regular Asdf

sdfasadfsdf

Uploaded by

santo nino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

Regular Asdf

sdfasadfsdf

Uploaded by

santo nino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Regular Expressions

Regular expressions are patterns that allow you to describe, match, or parse text. With regular
expressions, you can do things like find and replace text, verify that input data follows the format
required, and and other similar things.
Here's a scenario: you want to verify that the telephone number entered by a user on a form matches a
format, say, ###-###-#### (where # represents a number). One way to solve this could be:
Alternatively, we can use a regular expression here like this:
function isPattern(userInput) {
return /^\d{3}-\d{3}-\d{4}$/.test(userInput);
}
Notice how we’ve refactored the code using regex. Amazing right? That is the power of regular
expressions.
How to Create A Regular Expression
In JavaScript, you can create a regular expression in either of two ways:
 Method #1: using a regular expression literal. This consists of a pattern enclosed in forward
slashes. You can write this with or without a flag (we will see what flag means shortly). The
syntax is as follows:
const regExpLiteral = /pattern/; // Without flags

const regExpLiteralWithFlags = /pattern/; // With flags


The forward slashes /…/ indicate that we are creating a regular expression pattern, just the same way
you use quotes “ ” to create a string.
 Method #2: using the RegExp constructor function. The syntax is as follows:
new RegExp(pattern [, flags])
Here, the pattern is enclosed in quotes, the same as the flag parameter, which is optional.
So when do you use each of these pattern?
You should use a regex literal when you know the regular expression pattern at the time of writing the
code.
On the other hand, use the Regex constructor if the regex pattern is to be created dynamically. Also, the
regex constructor lets you write a pattern using a template literal, but this is not possible with the regex
literal syntax.
What are Regular Expression Flags?
Flags or modifiers are characters that enable advanced search features including case-insensitive and
global searching. You can use them individually or collectively. Some commonly used ones are:
 g is used for global search which means the search will not return after the first match.
 i is used for case-insensitive search meaning that a match can occur regardless of the casing.
 m is used for multiline search.
 u is used for Unicode search.
Let’s look at some regular expression patterns using both syntaxes.
How to use a regular expression literal:
// Syntax: /pattern/flags

const regExpStr = 'Hello world! hello there';

const regExpLiteral = /Hello/gi;

console.log(regExpStr.match(regExpLiteral));
// Output: ['Hello', 'hello']
Note that if we did not flag the pattern with i, only Hello will be returned.
The pattern /Hello/ is an example of a simple pattern. A simple pattern consists of characters that must
appear literally in the target text. For a match to occur, the target text must follow the same sequence
as the pattern.
For example, if you re-write the text in the previous example and try to match it:
const regExpLiteral = /Hello/gi;

You might also like