这样用,只会替换匹配到的第一个子串

str = 'I hava a pen ,I hava an apple,apple pen, pen apple'
str = str.replace('apple', 'pear')
//str='I hava a pen ,I hava an pear,apple pen, pen apple'

  

加上全局标识(g),替换全部子串

str = 'I hava a pen ,I hava an apple,apple pen, pen apple'
str = str.replace(/apple/g, 'pear')
//str='I hava a pen ,I hava an pear,pear pen, pen pear'

  

如果子串中有特殊字符,那么需要转义:
/static\/article_img/g
/static\.article_img/g

  


最新文章

  1. Eclipse调试 : step into,step over,step return 说明
  2. perl 二维数组
  3. sprint3(第四天)
  4. 最新php环境搭建
  5. API:System V & POSI
  6. tesseract 字体训练资料篇
  7. Zend Framework学习日记(1)--环境搭建篇(转)
  8. <转载>构造函数与拷贝构造函数
  9. vs2010 中检测到有潜在危险的 Request.Form 值
  10. 【Error】JavaWeb: 严重: Failed to initialize end point associated with ProtocolHandler ["http-bio-8080"]
  11. HDU 3681 BFS&像缩进DP&二分法
  12. GTK+2.0学习——code::block使用
  13. IOS中的绘图Quartz2D
  14. [转帖]SAP一句话入门:Human Resource
  15. 前缀和的应用 CodeForces - 932B Recursive Queries
  16. Tomcat不加项目名称访问设置
  17. 1.golang的环境搭建及入门
  18. 调用get_str_time(时间), 就能把毫秒的时间转换成格式化的 ,转化时间戳的方法
  19. SQL记录-PLSQL变量与常量文字
  20. MsChart,饼状图

热门文章

  1. Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---适配器模式之TurkeyAdapter[转]
  2. 计蒜客 Flashing Fluorescents(状压DP)
  3. 洛谷P2859 [USACO06FEB]摊位预订Stall Reservations
  4. 优化 Tengine HTTPS 握手时间
  5. TSQL:让监控分析更简单更高效
  6. Js Array 删除
  7. python输出最大公约数和最小公倍数
  8. vue-waterfall2 基于Vue.js 瀑布流组件
  9. 【水滴石穿】ReactNativeMobxFrame
  10. Effective Modern C++:07并发API