How to replace characters in javascript.

Posted by Sahoo under JavaScript on 3/20/2012 | Points: 10 | Views : 2066 | Status : [Member] | Replies : 5
Hi,

Can any one suggest how to replace some characters in JavaScript.Like

my original string is: ABC@#@DEF@#@@#@HUMAN BEING
MY REQUIREMENT: ABC1DEF11HUMAN BEING.
I tried a lot but not successful.




Responses

Posted by: Rajni.Shekhar on: 3/20/2012 [Member] Bronze | Points: 25

Up
0
Down
by using
str.replace("@#@", "");

Ex:

var str = "ABC@#@DEF@#@@#@HUMANBEING";
str = ((str.replace("@#@", "")).replace("@#@","")).replace("@#@","");

Thanks,
Rajni Shekhar

Sahoo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 3/20/2012 [Member] Bronze | Points: 25

Up
0
Down
var Ostring= document.getelementbyid("inpput1 ")value;

var dupString= document.getelementbyid("inpput2")value;
var OutChat= document.getelementbyid("out")value;
var Changed= Ostring.split(inChar);
Changednew= Changed.join(outChar);
document.getelementbyid("outnew")value = Changednew;


Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Sahoo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Arun.Padmanaban on: 3/20/2012 [Member] Starter | Points: 25

Up
0
Down
onclick of Replace button it will call the javascript(repll)



function repll() {
var findt = document.getElementById('findtext').value.trim();
var findin = document.getElementById('TextBox1').value.trim();
var findrep = document.getElementById('replace').value.trim();
var findIndex = findin.indexOf(findt, indexsta);
var charLength = findt.length;
if (findIndex > 0) {
indexsta = findIndex;
findin = setCharAt(findin, indexsta, findrep);
}
}
function setCharAt(findin, index, chr) {
if (index > findin.length - 1) return findin;
var replacethetext = findin.substr(0, index) + chr + findin.substr(index + 1);
document.getElementById('TextBox1').value = replacethetext;
return replacethetext;
}




<asp:TextBox ID="TextBox1" Text="testing the find and replace function .testing :P"
runat="server" Width="417px"></asp:TextBox>
<asp:TextBox ID="findtext" runat="server"></asp:TextBox>
<asp:TextBox ID="replace" runat="server"></asp:TextBox>
<input type="button" id="btnrep" value="Replace" onclick="repll()" />

Sahoo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 3/21/2012 [Member] Bronze | Points: 25

Up
0
Down
Arun.Padmanaban
Good
Code Lengthy

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Sahoo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sahoo on: 3/21/2012 [Member] Starter | Points: 25

Up
0
Down
Hi ALL,

Thanks 4 replay.
But I need one regular expression in JavaScript for it.

Sahoo, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response