Javascript用正则表达式replace替换父串中所有符合条件的子串
2024-08-29 20:00:56
这样用,只会替换匹配到的第一个子串
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
最新文章
- Eclipse调试 : step into,step over,step return 说明
- perl 二维数组
- sprint3(第四天)
- 最新php环境搭建
- API:System V &; POSI
- tesseract 字体训练资料篇
- Zend Framework学习日记(1)--环境搭建篇(转)
- <;转载>;构造函数与拷贝构造函数
- vs2010 中检测到有潜在危险的 Request.Form 值
- 【Error】JavaWeb: 严重: Failed to initialize end point associated with ProtocolHandler [&;quot;http-bio-8080&;quot;]
- HDU 3681 BFS&;amp;像缩进DP&;amp;二分法
- GTK+2.0学习——code::block使用
- IOS中的绘图Quartz2D
- [转帖]SAP一句话入门:Human Resource
- 前缀和的应用 CodeForces - 932B Recursive Queries
- Tomcat不加项目名称访问设置
- 1.golang的环境搭建及入门
- 调用get_str_time(时间), 就能把毫秒的时间转换成格式化的 ,转化时间戳的方法
- SQL记录-PLSQL变量与常量文字
- MsChart,饼状图
热门文章
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---适配器模式之TurkeyAdapter[转]
- 计蒜客 Flashing Fluorescents(状压DP)
- 洛谷P2859 [USACO06FEB]摊位预订Stall Reservations
- 优化 Tengine HTTPS 握手时间
- TSQL:让监控分析更简单更高效
- Js Array 删除
- python输出最大公约数和最小公倍数
- vue-waterfall2 基于Vue.js 瀑布流组件
- 【水滴石穿】ReactNativeMobxFrame
- Effective Modern C++:07并发API