js中对省市区字符串进行切割

JavaScript011

js中对省市区字符串进行切割,第1张

      // var address = "辽宁省沈阳市和平区"// 以这个地址为例      // const address = msg.detail       // const proExp = ".+[省]",cityExp = ".+[市]",disExp = ".+[区]"       // const province = address.match(new RegExp(proExp))// 省       // const city = address.match(new RegExp(cityExp))[0].replace(province, "")// 市       // const district = address.match(new RegExp(disExp))[0].replace(province, "").replace(city, "")// 区

你好!使用JS的正则可以进行处理,获取后用JS进行跳转,以下是我为你写的测试代码。希望能帮助到你。<script type="text/javascript">

function aaa()

{

//测试使用

var s1 = "f http://ubooo.com/test/123.html"

var s2 = " http://ubooo.com/test/123_2.html"

var reg = /\/([\d]+)(\.|[\d_]+\.)html/

var arr = s1.match(reg)

if (arr != null)

{

var url = " http://ubooo.com/Article.php?id=" + arr[1]

alert(url)

}//正常使用用以下代码

/*

var localUrl = location.href//获取当前URL地址

var reg = /\/([\d]+)(\.|[\d_]+\.)html/

var arr = localUrl.match(reg)

if (arr != null)

{

alert(1)

var url = " http://ubooo.com/Article.php?id=" + arr[1]

location.href = url//跳转

}

*/

}

</script>

<input type="button" value="测试" onclick="aaa()"/>

var address = '辽宁省沈阳市和平区' // 以这个地址为例

var proExp = '.+[省]', cityExp = '.+[市]', disExp = '.+[区]'

var province = address.match(new RegExp(proExp)) // 省

var city = address.match(new RegExp(cityExp))[0].replace(province, '') // 市

var district = address.match(new RegExp(disExp))[0].replace(province, '').replace(city, '') // 区