home ~ projects ~ socials

Find and Replace with String or RegEx

The simplest replacement is substituting one string for another with `.replaceAll()`

const original = "the quick brown fox"

const updated = original.replaceAll('brown', 'green')

console.log(updated)
Output:
the quick green fox

This can also be done with a regular expression

TODO: See also: .replace() which only does one

This is an example using a regex. You can also use a string. TKTKTK make more examples including ones with functions and strings

const regex = /\d+s/g

const original = "There are 1000s and 1000s of corgis"

const updated = original.replace(regex, 'lots')

console.log(updated)
Output:
There are lots and lots of corgis

More here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

-- end of line --