Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How make lodash _.replace all occurrence in a string?

How to replace each occurrence of a string pattern in a string by another string?

var text = "azertyazerty"; _.replace(text,"az","qu") 

return quertyazerty

like image 520
Anthony Avatar asked Apr 05 '16 14:04

Anthony


People also ask

How do you replace all occurrences in a string?

To replace all occurrences of a substring in a string by a new one, you can use the replace() or replaceAll() method: replace() : turn the substring into a regular expression and use the g flag.

Does replace remove all occurrences?

After execution, the replace() method returns a copy of the string that is given as input. In the output string, all the characters are replaced with the new character. To remove all the occurrences of a given character from a string, we will invoke the replace() method on the string.

How do you check for Lodash strings?

isString() Method. The _. isString() method is used to find whether the given value is a string object or not. It returns True if the given value is a string.


1 Answers

You can also do

var text = "azertyazerty"; var result = _.replace(text, /az/g, "qu"); 
like image 112
mrstebo Avatar answered Sep 26 '22 14:09

mrstebo