最佳JavaScript截取省市区


    var str = "湖北省武汉市江夏区文化大道110号";
    // var str ="内蒙古自治区乌兰浩特市二区"
    // var str = "重庆市渝中区中兴路"
    // var str = "湖北省黄石市阳新县"
    // var str ="湖北省宜昌市长阳土家族自治县"
    // var str = "重庆市秀山土家族苗族自治县武陵南路武陵广场"
 
    console.log(that.getArea(str));
 
//省市区截取
  getArea: function(str) {
    let area = {}
    let index11 = 0
    let index1 = str.indexOf("省")
    if (index1 == -1) {
      index11 = str.indexOf("自治区")
      if (index11 != -1) {
        area.Province = str.substring(0, index11 + 3)
      } else {
        area.Province = str.substring(0, 0)
      }
    } else {
      area.Province = str.substring(0, index1 + 1)
    }
 
    let index2 = str.indexOf("市")
    if (index11 == -1) {
      area.City = str.substring(index11 + 1, index2 + 1)
    } else {
      if (index11 == 0) {
        area.City = str.substring(index1 + 1, index2 + 1)
      } else {
        area.City = str.substring(index11 + 3, index2 + 1)
      }
    }
 
    let index3 = str.lastIndexOf("区")
    if (index3 == -1) {
      index3 = str.indexOf("县")
      area.Country = str.substring(index2 + 1, index3 + 1)
    } else {
      area.Country = str.substring(index2 + 1, index3 + 1)
    }
    return area;
  }

猜你喜欢

转载自blog.csdn.net/qq_38698753/article/details/90666712