treckstar

Performs multiple replacements on a single string - TS

May 25th, 2023
985
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 0.63 KB | Source Code | 0 0
  1. /**
  2.  * Performs multiple replacements on a single string.
  3.  * @param {string} str - The original string in which the replacements will be performed.
  4.  * @param {[string, string][]} replacements - An array of tuples containing the search text and its corresponding replacement text.
  5.  * @returns {string} The modified string with the replacements applied.
  6.  */
  7. function replaceMultipleValues(str: string, replacements: [string, string][]): string {
  8.   let result = str;
  9.  
  10.   for (const [searchText, replacementText] of replacements) {
  11.     result = result.replace(new RegExp(searchText, 'g'), replacementText);
  12.   }
  13.  
  14.   return result;
  15. }
  16.  
Advertisement
Add Comment
Please, Sign In to add comment