JavaScript String Type

Replace

We can replace text inside a string by using the replace method. The method can match the string(s) to replace either with a string search word or a RegExp.

If the replacing pattern we give is a string, only the first occurence of the will be replaced. We can use the replaceAll method to replace all occurrences of a string.

const wrongTitle = "The Man From Mars"
const title = wrongTitle.replace("Man From Mars",
"Martian")
console.log(title) // "The Martian"

Read more on MDN

String.prototype.replace()

Test yourself

Description

Define a new function with the name

kawaiify

that takes a string input parameter and replaces the first occurrence of

What

with

NANI

Tests

  1. 1 You should define a function named kawaiify.
  2. 2kawaiify("What!") should return "NANI!".
  3. 3kawaiify("A sparkling vampire. What!?") should return "A sparkling vampire. NANI!?".
  4. 4kawaiify("Say: What What") should return "Say: NANI What".
  5. 5kawaiify("In space, no one can hear you scream like a little girl.") should return "In space, no one can hear you scream like a little girl.".
Editor