Posted in Javascript onJuly 23, 2013
/** * 替换字符串中所有 * @param obj 原字符串 * @param str1 替换规则 * @param str2 替换成什么 * @return 替换后的字符串 */ function replaceAll(obj,str1,str2){ var result = obj.replace(eval("/"+str1+"/gi"),str2); return result; }
例如:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title> New Document </title> <meta name="Generator" content="EditPlus"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <script type="text/javascript"> function replaceAll(obj,str1,str2){ var result = obj.replace(eval("/"+str1+"/gi"),str2); return result; } alert(replaceAll("111,222,333",",","|")); </script> </head> <body> </body> </html>
js替换字符串的所有示例代码
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@