js截取字符串中的数字

时间:2021-09-02 06:17:54   收藏:0   阅读:23

今天项目中遇到一个截取字符串中的月份问题,再将月份作为路由参数传递到其他组件进行搜索,记录一下

1. 使用parseInt()

  let str = "7月份";
  let num = parseInt(str);
  console.log(num); //7

使用局限性:只能截取开头为数字的字符串,遇到非数字将停止截取

2. 使用正则表达式截取

  var str ="今年7月的月报资料";
  var num= str.replace(/[^0-9]/ig,"");
  console.log(num);//7

原文:https://www.cnblogs.com/ITwj-115/p/15206603.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!