replaceAllMatches
replaceAllMatches
(search: regex | string, replacement: string) => (aString: string) => string
Replace all matches of a string or regex.
When passing a regex, the global flag is required e.g.
/dog/g
const replaceDogWithCat = replaceAllMatches('dog', 'cat') replaceDogWithCat('This is my dog Pete. Pete is a good dog.') // is 'This is my cat Pete. Pete is a good cat.'
const replaceDogWithCat = replaceAllMatches('dog', 'cat') replaceDogWithCat('This is my dog Pete. Pete is a good dog.') // is 'This is my cat Pete. Pete is a good cat.'
const replaceDogWithCat = replaceAllMatches( 'dog', 'cat' ) replaceDogWithCat( 'This is my dog Pete. Pete is a good dog.' ) // is 'This is my cat Pete. Pete is a good cat.'
const replaceDogWithCat = replaceAllMatches( 'dog', 'cat' ) replaceDogWithCat( 'This is my dog Pete. Pete is a good dog.' ) // is 'This is my cat Pete. Pete is a good cat.'
Sometimes we want to replace strings in text. Below, we are an agency supporting travelers from the US going to Spain and France. People don't realize the degrees are Celsius in the weather reports, so let's quickly add 'C' after each degree.
const weatherReportByCountry = { spain: 'Today it will be sunny with a high of 33°.' + ' Tomorrow we get a breather, where the high will dip down to 28°.', france: 'Today it will be cloudy with a high of 29°.' + ' Tomorrow it clears up but drops down a little with a high of 26°.', } const clarifyCelsius = replaceAllMatches(/(?<deg>\d+°)/g, '$<deg>C') const clarifyReports = mapValues(clarifyCelsius) const { spain, france } = clarifyReports(weatherReportByCountry) console.log(spain) // prints // Today it will be sunny with a high of 33°C. Tomorrow we get a breather, where // the high will dip down to 28°C. console.log('\n' + france) // prints // Today it will be cloudy with a high of 29°C. Tomorrow it clears up but drops // down a little with a high of 26°C.
type Reports = Record<string, string> const weatherReportByCountry = { spain: 'Today it will be sunny with a high of 33°.' + ' Tomorrow we get a breather, where the high will dip down to 28°.', france: 'Today it will be cloudy with a high of 29°.' + ' Tomorrow it clears up but drops down a little with a high of 26°.', } const clarifyCelsius = replaceAllMatches(/(?<deg>\d+°)/g, '$<deg>C') const clarifyReports = mapValues(clarifyCelsius)<Reports> const { spain, france } = clarifyReports(weatherReportByCountry) console.log(spain) // prints // Today it will be sunny with a high of 33°C. Tomorrow we get a breather, where // the high will dip down to 28°C. console.log('\n' + france) // prints // Today it will be cloudy with a high of 29°C. Tomorrow it clears up but drops // down a little with a high of 26°C.
const weatherReportByCountry = { spain: 'Today it will be sunny with a high of 33°.' + ' Tomorrow we get a breather, where the high will dip down to 28°.', france: 'Today it will be cloudy with a high of 29°.' + ' Tomorrow it clears up but drops down a little with a high of 26°.', } const clarifyCelsius = replaceAllMatches( /(?<deg>\d+°)/g, '$<deg>C' ) const clarifyReports = mapValues(clarifyCelsius) const { spain, france } = clarifyReports( weatherReportByCountry ) console.log(spain) // prints // Today it will be sunny with a high of 33°C. // Tomorrow we get a breather, where the high will // dip down to 28°C. console.log('\n' + france) // prints // Today it will be cloudy with a high of 29°C. // Tomorrow it clears up but drops down a little // with a high of 26°C.
type Reports = Record<string, string> const weatherReportByCountry = { spain: 'Today it will be sunny with a high of 33°.' + ' Tomorrow we get a breather, where the high will dip down to 28°.', france: 'Today it will be cloudy with a high of 29°.' + ' Tomorrow it clears up but drops down a little with a high of 26°.', } const clarifyCelsius = replaceAllMatches( /(?<deg>\d+°)/g, '$<deg>C' ) const clarifyReports = mapValues( clarifyCelsius )<Reports> const { spain, france } = clarifyReports( weatherReportByCountry ) console.log(spain) // prints // Today it will be sunny with a high of 33°C. // Tomorrow we get a breather, where the high will // dip down to 28°C. console.log('\n' + france) // prints // Today it will be cloudy with a high of 29°C. // Tomorrow it clears up but drops down a little // with a high of 26°C.