Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot replace all ^ symbol to 25%5E

I have replace the problem in react. I want ^ to 25%5E, but I just can replace one of ^ in the string, cannot replace all ^ symbol to 25%5E. Below is my sample code:

var urlStr = "http://localhost:3005/branch-management/edit-branch/?companyName=ABC%20SDN%20BHD%20!!!!%40%40%40%40%23%24%^%26*()&branchName=ABC%20!%40%23%24%^%26*()_";
var newUrlStr = urlStr.replace("^", "25%5E");

console.log(newUrlStr);

Error Result:

img

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Hope someone can guide me on how to solve this problem. Thanks.

>Solution :

You should use replaceAll which is for replacing all matched strings

var urlStr = "http://localhost:3005/branch-management/edit-branch/?companyName=ABC%20SDN%20BHD%20!!!!%40%40%40%40%23%24%^%26*()&branchName=ABC%20!%40%23%24%^%26*()_";
var newUrlStr = urlStr.replaceAll("^", "25%5E");

console.log(newUrlStr);

You also can use regex with /g (a global flag) to have similar behaviour

    var urlStr = "http://localhost:3005/branch-management/edit-branch/?companyName=ABC%20SDN%20BHD%20!!!!%40%40%40%40%23%24%^%26*()&branchName=ABC%20!%40%23%24%^%26*()_";
var newUrlStr = urlStr.replace(/\^/g, "25%5E");

console.log(newUrlStr);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading