let str = "one two three";
let fixRegex = /three/; // Change this line
let replaceText = (fixRegex,"three two one"); // Change this line
let result = str.replace(fixRegex, replaceText);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36.
Challenge: Use Capture Groups to Search and Replace
the code I create therein is not going through,I’ve tried solving it but still no advancement .please am really in need of help cos I don’t wanna stuck up.
Hey @Ahmadasm326
We have to save a regex in the variable fixRegex able to match the three words, you could create three different Regex as well but the challenge here is meant to show you how to use a Capture Groups approach where you create different matches groups in the Regex (\w+) to then swap the order using the replace method in the last line.
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);
Does it make sense now? It is important to understand what is behind the "$3 $2 $1" here, so let me know if it is not clear and I am more than happy to help you out in understanding this concept
Happy Coding