Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

urgent: javascript replace() method not accepting variables

Status
Not open for further replies.

saiteja

Programmer
Sep 24, 2003
3
0
0
IN
Hi,

I am using replace method in my javascript function with regular expression. The following is the function.

<script language=&quot;javascript&quot;>
var rowNum = 1;
function addRow()
{
tabBody = document.getElementById('insert').firstChild;
newRow = tabBody.appendChild(tabBody.firstChild.cloneNode(true));
rowNum = rowNum + 1;
for(i=0;i<newRow.cells.length;i++)
{
newRow.cells.innerHTML = newRow.cells.innerHTML.replace(/1([^\d])/g, rowNum+'$1');
}
}
</script>

The above function creates a new row in the table and increments the column name by 1. However, in the above function, if I pass a variable in place of 1, this does not work. Can anyone help me how to do this.
var temp=1;
{
newRow.cells.innerHTML = newRow.cells.innerHTML.replace(/temp([^\d])/g, rowNum+'$1');
}

My purpose is that I have some 4 rows in a upper table above this and want this table to start a new row with column name as 5. So I am passing the no. of rows in the above in a variable to this function. However, the replace function is not taking it.

Thanks,
Krishna
 
is this what u want?:
/temp[\d]/g

what is the use of ([^\d])?

Known is handfull, Unknown is worldfull
 
how did u do it?

Known is handfull, Unknown is worldfull
 
vbkris,

The only way I found to incorp. a variable
into to a reg exp is to use new RegExp()

If there is another way that's easier I would
welcome the info.

var restr = temp +'[^\d]';
var restrb = new RegExp(restr,&quot;g&quot;);
var newLine = wstr.replace(restrb,nstr)

2b||!2b
 
yes there is. its the .match() method.
s=&quot;ASDASDASD&quot;
if(s.match(/^A/))
{
alert(&quot;Yo&quot;)
}

Known is handfull, Unknown is worldfull
 
oops:
if(!s.match(/^A/))
{
alert(&quot;A is there&quot;)
}

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top