Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Find and Replace with String or RegEx

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

Code

const original = "the quick brown fox"

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

console.log(updated)

Results

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

Code

const regex = /\d+s/g

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

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

console.log(updated)

Results

There are lots and lots of corgis

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