Plz help me, i want to use the replace function by passing variable parameters e.g.
str1="abcdefg";
str2="ab";
str1.replace("str1"i, str2);
i have tried this but it doesnt work because the first parameter should be RegExp. I dont know how to get RegExp of str1 and then pass it as a parameter in replace function. plz help
<script type="text/javascript">
var str1="abcdefg";
var str2="ab";
var t= str1.replace(str1, str2);
alert(t); // ab
// or
var re= RegExp(str1,"i");
var n= str1.replace(re, str2);
alert(n); // ab
</script>
Or
Code:
<script type="text/javascript">
RegExp.prototype.talha=function(str2){
return str2.replace(this,str2);
}
var str1="abcdefg";
var str2="ab";
var re= RegExp(str1,"i");
var s= re.talha(str2);
alert(s); // ab
</script>
Last edited by Ayşe; 02-20-2009 at 12:29 PM.
The Time Through Ages
In the Name of Allah, Most Gracious, Most Merciful
1. By the Time,
2. Verily Man is in loss,
3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.
<script type="text/javascript">
RegExp.prototype.talha=function(str1, str2){
return str1.replace(this,str2);
}
var str1="abcdefg";
var str2="ab";
var re= RegExp(str1,"i");
var s= re.talha(str1, str2);
alert(s); // ab
</script>
When I noticed my error in my code, I want to edit my message instead of sending a new message.
Last edited by Ayşe; 02-21-2009 at 02:47 AM.
The Time Through Ages
In the Name of Allah, Most Gracious, Most Merciful
1. By the Time,
2. Verily Man is in loss,
3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.
The Time Through Ages
In the Name of Allah, Most Gracious, Most Merciful
1. By the Time,
2. Verily Man is in loss,
3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.
Bookmarks