java和js 用正则表达式切割省市县的代码
String s= "安徽省合肥市肥西县上派镇"; // String regex="(\\?[^省]+自治区|.*?省|.*?行政区|.*?市)(\\?[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(\\?[^县]+县|.+?区|.+市|.+旗|.+海域|.+岛)?(\\?[^区]+区|.+镇)?(\\?.*)"; String regex="((?<province>[^省]+省|.+自治区)|上海|北京|天津|重庆)(?<city>[^市]+市|.+自治州)(?<county>[^县]+县|.+区|.+旗|.+海域|.+岛)?(?<town>[^乡].+镇.+乡)?(?<village>.*)"; Matcher m=Pattern.compile(regex).matcher(s); // Matcher m = r.matcher(s); while (m.find()) { System.out.println("Found value: " + m.group(0) ); System.out.println("Found value: " + m.group(1) ); System.out.println("Found value: " + m.group(2) ); System.out.println("Found value: " + m.group(3) ); System.out.println("1111111111111111111111111111111111111"); System.out.println("Found value: " + m.group("province") ); System.out.println("Found value: " + m.group("city") ); System.out.println("Found value: " + m.group("county") ); System.out.println("Found value: " + m.group("town") ); System.out.println("Found value: " + m.group("village") ); } System.out.println("NO MATCH");
vue
//select
<el-form-item label="省市区" prop="shszssq">
<v-distpicker v-model="form.shszssq"
:province="select.province"
:city="select.city"
:area="select.area"
@selected="selected"
></v-distpicker>
</el-form-item>
//省市区截取 selected(data){ this.form.shszssq = data.province.value+data.city.value+data.area.value; },
//省市区截取 getArea(str) { var str = str; var reg = /.+?(省|市|自治区|自治州|行政区|盟|旗|县|区)/g // 省市区的正则 const area = str.match(reg) // 分割省市区 // console.log(area) // ['内蒙古自治区', '锡林郭勒盟', '正镶白旗'] /*var len = 0 for (var i = 0; i < area.length; i++) { // 获取省市区的长度 len += area[i].length } area.push(str.substring(len)) // 获取省市区之后的字符串 console.log(area) // ['内蒙古自治区', '锡林郭勒盟', '正镶白旗', '前进大街2699号'] for (var j = 0; j < area.length; j++) { console.log(area[j]) }*/ return area; },
var area =this.getArea(this.form.shszssq);
this.select ={ province: area[0], city: area[1], area: area[2] }