replace function in javascript with regularexpression

Possible Duplicate:
How do you pass a variable to a Regular Expression JavaScript?
javascript regular expressions with variables?

I want to create a function which replace text in javascript using Regular expression like

   function(str, source, target)
   {
     /* str = string, source = text to replace, target = text with replace */
     str.replace(/source/i, target);
   }

Now my problem is here this function is treating source as a text not as a variable. How can i fix this?


您正在寻找:

var re = new RegExp(source, "i");
return str.replace(re, target);
链接地址: http://www.djcxy.com/p/76840.html

上一篇: 带有变量的正则表达式

下一篇: 用正则表达式替换javascript中的函数