Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript regex replace all

const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';

console.log(p.replaceAll('dog', 'monkey'));
// expected output: "The quick brown fox jumps over the lazy monkey. If the monkey reacted, was it really lazy?"


// global flag required when calling replaceAll with regex
const regex = /Dog/ig;
console.log(p.replaceAll(regex, 'ferret'));

// expected output: "The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?"
Comment

replace all with regex

const regex = new RegExp(oldString, 'g');
const replacedWord = stringToBeChanged.replace(regex, newString);
Comment

PREVIOUS NEXT
Code Example
Javascript :: mongoose save or update 
Javascript :: how to insert an item into an array at a specific index in javascript 
Javascript :: useroutematch 
Javascript :: vue access computed property in data 
Javascript :: Integrating Axios with React Hooks 
Javascript :: canvas rounded corners on image 
Javascript :: website implement jquery in js 
Javascript :: react native image 
Javascript :: dynamic import in reactjs 
Javascript :: vue custom events 
Javascript :: js countdown 
Javascript :: dynamic navigation with subitems 
Javascript :: how to get array from object in javascript 
Javascript :: rock paper scissors javascript 
Javascript :: set lodash 
Javascript :: require mongoose 
Javascript :: js mouse move activate 
Javascript :: min heap javascript 
Javascript :: inverse of stringify js 
Javascript :: type in javascript 
Javascript :: delete space from string javascript 
Javascript :: ERROR in ./node_modules/react-icons/all.js 4:0-22 
Javascript :: pass variable to partial view ejs 
Javascript :: jquery append to table 
Javascript :: angular formgroup on value change 
Javascript :: Dart regex all matches 
Javascript :: js alert new line 
Javascript :: timeout angularjs 
Javascript :: for of loop 
Javascript :: image view component react js 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =