Click here to Skip to main content
15,904,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How can i remove a character using javascript ?

I want to remove all occurrence of a character from my string. how can i do this?
when i use replace it is removing only first occurrence.
Posted

Hello,

Please have a look at W3Schools[^].

Regards,
 
Share this answer
 
Comments
Am Gayathri 19-Nov-14 9:39am    
This will replace only first occurrence.
Am Gayathri 19-Nov-14 9:41am    
I want to remove * from my text. I can not use str.replace(/blue/g, "red");
Prasad Khandekar 19-Nov-14 9:50am    
You can use RegExp("YourChar", "g") e.g.

var txt = document.getElementById('sometextbox').value;
var rechar = document.getElementById('replaceCharTextbox').value;
var srchChar = document.getElementById('searchCharTextbox').value;
var regx = new RegExp(srchChar, "g");
txt = txt.replace(regx, rechar);
Am Gayathri 19-Nov-14 9:51am    
I want to remove * from my string. Cannot use regular expressions. When i use like str.replace(/*/g, "red"); to remove *.
Code is getting commented. ( Javascript '/*' means comment the code)
Prasad Khandekar 19-Nov-14 9:57am    
use /\*/g as your search pattern.
 
Share this answer
 
Comments
Am Gayathri 19-Nov-14 9:49am    
I want to remove * from my string. Cannot use regular expressions. When i use like str.replace(/*/g, "red"); to remove *.
Code is getting commented. ( Javascript '/*' means comment the code)
Use regex like this:
XML
<script>
var s = "dfaskfjkd  d a";
s = s.replace(/[a]+/g, '');
alert(s);
</script>

which will remove all character 'a' in the string.
Learn more about The 30 Minute Regex Tutorial[^]
 
Share this answer
 
v2
Comments
Am Gayathri 19-Nov-14 9:53am    
Ya it works..Great
Thanks a lot...
Peter Leow 19-Nov-14 10:17am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900