Please help me based on javascript (characters swap) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Please help me based on javascript (characters swap)

Select two characters that exists in the string c1,c2.Replace all occurrences of c1 with c2 and all occurrences of c2 with c1 for example. "bbcacad"

17th Jan 2022, 9:24 AM
Manjula
5 Answers
+ 3
"bbcacad".replace(new RegExp(c1, "g"), c2); https://code.sololearn.com/WtM4qrG4t7yk JavaScript String replace() https://www.w3schools.com/jsref/jsref_replace.asp
17th Jan 2022, 10:41 AM
SoloProg
SoloProg - avatar
+ 2
let s = "bbcacad"; let r; r = s.replace(new RegExp('b', "g"), '#'); r = r.replace(new RegExp('a', "g"), 'b'); r = r.replace(new RegExp('#', "g"), 'a'); console.log(s, r);
17th Jan 2022, 12:03 PM
SoloProg
SoloProg - avatar
+ 1
Thank you
17th Jan 2022, 10:44 AM
Manjula
+ 1
In above example :bbcacad That we can execute like aacbcbd And aabcbcd
17th Jan 2022, 11:19 AM
Manjula
0
I will try this
17th Jan 2022, 10:43 AM
Manjula