How to replace each occurrence of a string pattern in a string by another string?
var text = "azertyazerty"; _.replace(text,"az","qu")
return quertyazerty
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.
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.
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.
You can also do
var text = "azertyazerty"; var result = _.replace(text, /az/g, "qu");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With