replace JavaScript

The JavaScript replace method is used on strings to replace one or more values.

The syntax is therefore the following: string.replace (value, newValue).

Then the method accepts two mandatory parameters, the first value representing the value to search for in the string and which will subsequently be replaced by the second parameter, newValue.

JavaScript replace – examples

In this first example we will replace one word with another.

Here is a possible implementation:



var sentence = 'coding since high school';
var sentenceChange = sentence.replace('high','primary');
console.log(sentenceChange);

In this way, if there are more occurrences, only the first one it encounters will be replaced. If we want to replace them all we can use the global operators.

JavaScript replace and Global operator g

With the replace method of JavaScript you can make sure that if there are more occurrences, they are all replaced using the global operator g, so we don’t stop at the first one.

This operator is used in conjunction with regular expressions which have the following syntax / pattern / modifiers. Then we enclose the value to be replaced in / and then add the modifier, in our case g.

Attention g is case-sensitive, so using this modifier does not substitute words that are not case-sensitive.

Here is an example of using the global operator g with the replace method of JavaScript.



var sentence = 'Coding since high school. In first grade high school and second grade High school ';

var sentenceChange = sentence.replace(/high/g,'primary');
console.log(sentenceChange);

In this case you will get this result: Coding since primary school. In first grade high school and second grade primary school.

So it did not replace Superiore as it has a capital letter.

JavaScript replace and Global operator gi

You can use the global operator g with i, where i is a modifier as it modifies the search so that it is not case sensitive.

The global operator gi is therefore case-insensitive, so used with the replace method of JavaScript it allows you to replace all occurrences regardless of whether they are uppercase or lowercase.

So here’s an example:



var sentence = 'Coding since high school. In first grade HIGH school and second grade high school ';

var sentenceChange = sentence.replace(/high/gi,'primary');
console.log(sentenceChange);

In this example, all the terms ‘High’ will be replaced, regardless of how they are written.

In this lesson we have seen some very simple examples of using replace in JavaScript and we have mentioned regular expressions. We will come back to talking about regular expressions further on in the JavaScript tutorial.

Some useful links

JavaScript tutorial

JavaScript Calculator

Object in JavaScript

For in loop

Caesar cipher decoder

Slot Machine in JavaScript

Introduction to JavaScript language

Learn JavaScript – basic concepts

JavaScript variables and costants

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *