need help with a "replace" function

N

namemattersnot

Here's my code:

###
myvar = "test1_1;test2_1;test3_1";
myvar = myvar.replace(/_1/g, "_2")';
###

it works just fine. if I introduce "cat = 1", how do I change my
replace function so that instead of replacing a fixed "1" value, I
would use "cat"?

myvar = myvar.replace('/_'+cat'/g', "_2")' does not work.

Thanks!
 
N

namemattersnot

figured it out myself:

myvar = "test1_1;test2_1;test3_1";
cat = "1"
myregexp = new RegExp("_"+cat, "g")
myvar = myvar.replace(myregexp, "_2")';

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top