Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

replaceAll vs replace vs split join

const source = "abcdefabcdef";
const str1 = "abc", str2 = "xyz";
const reg1 = /abc/g, reg2 = "xyz";

//Case 1 : When we want to replace a string by another
console.log(source.split(str1).join(str2));
console.log(source.replace(new RegExp(str1,"g"),str2));
//versus
console.log(source.replaceAll(str1,str2));

//Case 2 : When we want to use a regular expression
console.log(source.replace(reg1,reg2));
//versus
console.log(source.replaceAll(reg1,reg2));

//Result = "xyzdefxyzdef"
Comment

PREVIOUS NEXT
Code Example
Javascript :: string to char code array javascript 
Javascript :: javascript function call with variable 
Javascript :: how to change currency in react-paypal-button-v2 
Javascript :: expected an identifier and instead saw const 
Javascript :: add jquery through consol 
Javascript :: axios httsagent 
Javascript :: using javascript array create bootstrap card 
Javascript :: von click 
Javascript :: winston logger levels 
Javascript :: get date in format 
Javascript :: vue js qr code scanner 
Javascript :: Nestjs services update example 
Javascript :: react native elements bottom sheet close on back button press 
Javascript :: node start is too slow windows 10 
Javascript :: document middleware in express 
Javascript :: vuejs pass data to router-view 
Javascript :: jquery toggle visibility 
Javascript :: setTimeout() nodejs 
Javascript :: converting circular structure to json 
Javascript :: ~~ in javascript 
Javascript :: leafletjs code 
Javascript :: graphql yoga access http headers 
Javascript :: remove parent element javascript 
Javascript :: how to hide footer in specefic pages in react router 
Javascript :: java.lang.IllegalArgumentException: Can only download HTTP/HTTPS 
Javascript :: react native shadow maker 
Javascript :: feet to inches 
Javascript :: get search value from reacr route1 
Javascript :: test one function in react class 
Javascript :: backdrop issue with multiple modal 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =