javascript中字符串string.replace 第二个参数是函数简单介绍

xiaoxiao2021-02-28  124

例子:

const camelizeRE = /-(\w)/g function camelize(str) { return str.replace(camelizeRE, toUpper) }; function toUpper (a, c,d) { console.log(arguments.length+";"+JSON.stringify(arguments)); console.log(a+";"+c+";"+d); return c ? c.toUpperCase() : '' } camelize('dfd-res-fd');`

上面例子中toUpper 的参数解析: 上面函数总共有4个参数(参数个数不固定的) 第一个参数是:匹配到的字符串;匹配到的是:-r ,-f 第二个参数是:正则表达式()匹配到的字符串 :r ,f 第三个参数是:匹配到字符串的起始位置index:3,7 第四个参数是:原始字符串 dfd-res-fd

转载请注明原文地址: https://www.6miu.com/read-22826.html

最新回复(0)