Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Using String Methods

In JavaScript, regular expressions are commonly used with two string methodssearch() and replace().

The search() method uses a regular expression to find a match and returns the position of the first match.

The replace() method returns a new string with the specified pattern replaced.

Using String search() With a String

The search() method searches a string for a specified value and returns the index of the first match.

Example

Use a string to search for code7school within another string.

let text = “Visit code7school!”;
let n = text.search(“code7school”);

The value stored in n will be:

6

Using String search() With a Regular Expression

Example

Use a regular expression to perform a case-insensitive search for “code7school” within a string.

let text = “Visit code7school”;
let n = text.search(/code7school/i);

The value stored in n will be:

6

Using String replace() With a String

The replace() method replaces a specified value in a string with a different value.

let text = “Visit Microsoft!”;
let result = text.replace(“Microsoft”“code7school”);

Use String replace() With a Regular Expression

Example

Use a case-insensitive regular expression to replace “Microsoft” with “code7school” in a string.

let text = “Visit Microsoft!”;
let result = text.replace(/microsoft/i“code7school”);

The value stored in res will be:

Visit code7school!

Did You Notice?

You can use regular expression arguments (instead of string arguments) in the methods mentioned above. Regular expressions enhance the power of your search, allowing for features like case insensitivity, among other capabilities.

Regular Expression Modifiers

Modifiers can be used to conduct case-insensitive or global searches.

Modifier

Description

i

Perform case-insensitive matching

g

Perform a global match (find all)

m

Perform multiline matching

d

Perform start and end matching (New in ES2022)

Regular Expression Patterns

Brackets are used to specify a range of characters to match.

Expression

Description

[abc]

Find any of the characters between the brackets

[0-9]

Find any of the digits between the brackets

(x|y)

Find any of the alternatives separated with |

Metacharacters are characters that have a special significance or function in regular expressions.

Metacharacter

Description

\d

Find a digit

\s

Find a whitespace character

\b

Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b

\uxxxx

Find the Unicode character specified by the hexadecimal number xxxx

Quantifiers define quantities:

Quantifier

Description

n+

Matches any string that contains at least one n

n*

Matches any string that contains zero or more occurrences of n

n?

Matches any string that contains zero or one occurrences of n

Using the RegExp Object

In JavaScript, the RegExp object is a regular expression that comes with predefined properties and methods.

Using test()

The test() method is a RegExp method that searches a string for a pattern and returns true or false based on whether a match is found.

The following example searches for the character “e” in a string:

Example

const pattern = /e/;
pattern.test(“The best things in life are free!”);

Since the string contains the character “e,” the output of the code above will be:

true

You don’t need to store the regular expression in a variable first. The two lines above can be condensed into a single line.

/e/.test(“The best things in life are free!”);

Using exec()

The exec() method is a RegExp method that searches a string for a specified pattern and returns the matched text as an object.

If no match is found, it returns null.

The following example searches for the character “e” in a string:

Example

/e/.exec(“The best things in life are free!”);