I want to remove special characters from a string and replace them with the _
character.
For example:
string = "img_realtime_tr~ading3$"
The resulting string should look like "img_realtime_tr_ading3_";
I need to replace those characters: & / \ # , + ( ) $ ~ % .. ' " : * ? < > { }
If you are having a string with special characters and want's to remove/replace them then you can use regex for that. Use this code: Regex. Replace(your String, @"[^0-9a-zA-Z]+", "")
To replace special characters, use replace() in JavaScript.
replace(/>/g, ">"). replace(/</g, "<"). replace(/"/g, """);
string = string.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_');
Alternatively, to change all characters except numbers and letters, try:
string = string.replace(/[^a-zA-Z0-9]/g,'_');
string = string.replace(/[\W_]/g, "_");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With