Regex 提取字符串中重复数据且格式化显示

方法:用   $下标    提取满足项的值

  /**
   * 餐食信息格式转换
   * @早餐,1@晚餐,2  =》  早餐(1份);晚餐(2份)
   */
  convertMealInfo = (mealInfo) => {
    if(mealInfo){
      return mealInfo.replace(/\@([\u4e00-\u9fa5]+)[,,]([0-9]+)/g,'$1($2份);')
    }
    return ''
  }

调用

convertMealInfo('@早餐,1@晚餐,2')

 

posted @ 2019-09-03 11:11  Robot-Blog  阅读(256)  评论(0编辑  收藏  举报