美文网首页
window.location属性

window.location属性

作者: Willworkgogogo | 来源:发表于2017-03-14 10:59 被阅读0次

该属性返回url的解析

返回一个对象

location.search 获取query部分信息

window.location.search.match(/page=(\d+)/)       //return ["page=1", "1"], 为什么这样返回,下面有详细解释

附: String.prototype.match()

字符串和正则表达式匹配,查找匹配项。返回Array,如果没有匹配项返回null。

  • 未传入参数, 返回 空字符串数组, [""]
// 举一个完整的列子
var str = 'welcome to regular expression , see Chapter 3.4.5.1',
    reg = /see (chapter \d+(\.\d)*)/i;      // i 忽略大小写, g 全局搜索
    str.match(reg)
//return
//Array["see Chapter 3.4.5.1",  "Chapter 3.4.5.1",  ".1"] , index: 32,  input: 
//"welcome to regular expression , see Chapter 3.4.5.1"
返回的值中包含三部分: 数组、index、input
数组里的组成是根据正则的组成形式来返回, Array[0]是完整的匹配返回, 后面的就是正则括号里的小范围匹配返回; 
index作为返回的属性, 返回的是正则匹配字符串在原始字符串的起始下标;
input属性, 返回完整原始字符串
返回值

相关文章

网友评论

      本文标题:window.location属性

      本文链接:https://www.haomeiwen.com/subject/enpcnttx.html