Replace string in javascript with examples including regular expression REGEX

Опубликовано: 29 Сентябрь 2024
на канале: Tech Nursery
109
1

In this video, we are discussing How to replace a character,a substring or group of characters from a string variable in javascript

1. Replace a character from a variable with empty character
2.Replace a sub string from a variable with empty character
3.Replace a set of characters from a variable with empty character


Here we replace character 'e' from string 'Technursery' by calling blogName.replace('e', '') .It will replace first occurrence of 'e' from the string 'Technursery'

const blogName = 'Technursery';

const blogNameWithoutVowels = blogName.replace('e', '');

console.log(blogNameWithoutVowels);

Here we are replacing all vowels characters aeiou from the given string using regular expression REGEX.By executing blogName.replace(/[aeiou]/gi, '') we will get the string without all vowels aeiou

const blogName = 'Technursery';

const blogNameWithoutVowels = blogName.replace(/[aeiou]/gi, '');

console.log(blogNameWithoutVowels);

Here we replace a substring with empty string in javascript. When we execute blogName.replace('Tech', '') it will replace substring Tech with empty string

const blogName = 'Technursery';

const blogNameWithoutVowels = blogName.replace('Tech', '');

console.log(blogNameWithoutVowels);