Link Search Menu Expand Document

Use Capture Groups to Search and Replace

Summary

  • .replace can be used to search and replace text.

Final Code

let str = "one two three";
let fixRegex = /(\w+)\s(\w+)\s(\w+)/; // Change this line
let replaceText = "$3 $2 $1"; // Change this line
let result = str.replace(fixRegex, replaceText);