将字符串中的px单位转换为rem单位做移动端适配
/**
* @description: 正则表达式匹配 px 像素单位转换为 rem
* @params : str 字符串,width 基数
*/
export const pxToRem = (str, width = 37.5) => {
var reg = /(:")+(\d+(\.\d*)?)+(px")/gi; //可以匹配浮点数
let newStr = str.replace(reg, function (_x) {
_x = _x.replace(/:"/gi, '').replace(/px"/gi, '');
return ':"' + parseFloat(parseFloat(_x) / width) + 'rem"';
});
return newStr;
}