【JavaScript】正则表达式--String.replace()变量替换的一点笔记

1 2 3 4 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个用()包起来的表达式的值。 要用()包起来呀…‘(>﹏<)
相关文章
相关标签/搜索