View Full Version : Replacing a char at the end of a variable Name (JS)
Hello all!
I have a Variable that ends with a digit.
I want to change the digit with "+1" or with another var which I have..
I tried to do it with regex, but I failed.. (just didn't find the right way to write it I guess..)
So thanks to all the ppl who will help!
Buzi
coothead
07-26-2005, 12:31 PM
Hi there buzi,
and a warm welcome to these forums. :)
Does this help in any way...
<script type="text/javascript">
<!--
var str="stringWithDigit_7";
alert("this is the original var: \n\n..."+str); //here for testing
var strNum=parseFloat(str.charAt(str.length-1))+1;
str=str.replace(/\d/,strNum);
alert("this is the modified var: \n\n... "+str); //here for testing
//-->
</script>
Hey coothead, and thank you for both the welcoming and the answer!
I didn't explained myself enough I think so here it goes:
I have a table that has a couple of rows.
The first 3 rows are not relevant.
The next row has fields for a form. Each field has a name that ends with the number 1.
The user can click on a button which adds another row to the table.
This new row is a clone of the above row.
So..... I need to replace the fields names in the new row from the older row number, to the new number.
The function I have now is:
for (i=0;i<main_table.rows[wanted_row].cells.length;i++){
try{main_table.rows[wanted_row].cells[i].innerHTML=main_table.rows[wanted_row].cells[i].innerHTML.replace(/(id=\"{0,1}[a-z^_]+)/gi,"$1"+row_num);
main_table.rows[wanted_row].cells[i].innerHTML=main_table.rows[wanted_row].cells[i].innerHTML.replace(/(name=\"{0,1}[a-z^_]+)/gi,"$1"+row_num);
}
}
{
Lets say one of the variables name is "var_name1".
The regex in here finds the "var_name" and puts it in "$1".
So according to the replace function in here, the new var name will be "var_name21" (assuming "row_num=2").
So I need a way to remove what's after the $1, or find a completly other way for it.
And because your script has to have the variable name, I can't use it..
So you think you can still help me? :-)
Jon Hanlon
07-26-2005, 06:12 PM
You're better off modifying the actual properties rather than the innerHTML.
var cell = main_table.rows[wanted_row].cells[i];
cell.name = cootsFunction(cell.name);
cell.id = cootsFunction(cell.id);
function cootsFunction(str) {
var strNum=parseFloat(str.charAt(str.length-1))+1;
return str.replace(/\d/,strNum);
}
First of all, Thank you.
second-
All of that should be inside the loop?
if no, from where should it get the "i" ?
Jon Hanlon
07-26-2005, 06:28 PM
The function is outside the loop. The rest is inside.
I get an "undefined" error on this line:
var strNum=parseFloat(str.charAt(str.length-1))+1;
well, found my solution..
my problem was how to get rid of the last digit.
so I found out that the $1, $2 thingys are set by ().
so I made this:
innerHTML.replace(/(details_{0,1}\D*)(\d)/gi,"$1"+row_num);
so by ignoring from the $2, I can get rid of the the old digit and put the new one.
it's working and I'm happy.
now I have another question I've posted in the forum.
Thank you all for your help!
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.