【JavaScript】正则表达式--String.replace()变量替换的一点笔记
2016-06-15 22:20
756 查看
1234 | var re = /(\w+)\s(\w+)/;var str = 'John Smith';var newstr = str.replace(re, '$2, $1');console.log(newstr); // Smith, John |
$1
$2是指被括号
()包起来的
\w+。
即当要使用变量替换时,
$n指第n个用
()包起来的表达式的值。
要用
()包起来呀…‘(>﹏<)′ ‘(>﹏<)′Specifying a string as a parameterThe replacement string can include the following special replacement patterns:
Pattern | Inserts |
---|---|
$$ | Inserts a “$”. |
$& | Inserts the matched substring. |
$` | Inserts the portion of the string that precedes the matched substring. |
$’ | Inserts the portion of the string that follows the matched substring. |
$n | Where n is a non-negative integer lesser than 100, inserts the nth parenthesized submatch string, provided the first argument was a RegExp object. |
Switching words in a string
About Sodino相关文章推荐
- 24、DOM的高级应用1-------表格的动态删除和添加行的操作
- ajax输出json格式
- JavaScript 初级教程
- JavaScript实现图片轮播器
- js中的DOM操作上(参考慕课网)
- 理解JS函数中的参数
- rest接口设计之--返回值设计
- 远程服务器存储之JSON
- js页面跳转整理
- JavaScript实现选项卡效果
- js 类的简单实现与调用
- js中使用另外一个js文件的变量
- 安卓.点击头像-->编辑个人姓名-->提交后.同时调用js关闭页面-->返回上一层
- JS闭包那些事
- JSP中的MIME
- js函数(类)的继承机制的设计与实现(五)
- BZOJ4488: [Jsoi2015]最大公约数
- json和xml的区别
- js限制文本框只能输入数字方法小结
- JS的解析机制