^ $
位置符,标志起始与结束,以在匹配中使用。
/b
分隔字符,分隔单词边界
字符类别:\d \w \s 等
\d:匹配任意数字
\w:匹配任意数字+字母+下划线
\s:匹配任意空白符(空格、回车之类的)
均只能匹配一位
.
.:匹配任意单个字符,与其它字符联合起来使用。
数量词:+ * ?
+:出现一次或多次
*:出现一次或多次
?:出现零次或一次
分组:()
实例
解析网址
var url = 'http://snapshot.sogoucdn.com/websnapshot?&url=http%3A%2F%2Fwww.bxwx9.org%2Fbinfo%2F19%2F19765.htm&k=d0e9028572a1dec2f3842231a7037928&encodedQuery=&query=&w=01020400&pid=sogou-brse-1cdf14d1e3699d61&pid=AG8Og'
/^\w+:\/\//.exec(url)
0: "http://"
index: 0
input: "http://snapshot.sogoucdn.com/websnapshot?&url=http%3A%2F%2Fwww.bxwx9.org%2Fbinfo%2F19%2F19765.htm&k=d0e9028572a1dec2f3842231a7037928&encodedQuery=&query=&w=01020400&pid=sogou-brse-1cdf14d1e3699d61&pid=AG8Og"
length: 1
/^(\w+:)\/\//.exec(url)
0: "http://"
1: "http:"
index: 0
input: "http://snapshot.sogoucdn.com/websnapshot?&url=http%3A%2F%2Fwww.bxwx9.org%2Fbinfo%2F19%2F19765.htm&k=d0e9028572a1dec2f3842231a7037928&encodedQuery=&query=&w=01020400&pid=sogou-brse-1cdf14d1e3699d61&pid=AG8Og"
length: 2
/^(\w+:)\/\/([^\/]+)/.exec(url)
0: "http://snapshot.sogoucdn.com"
1: "http:"
2: "snapshot.sogoucdn.com"
index: 0
input: "http://snapshot.sogoucdn.com/websnapshot?&url=http%3A%2F%2Fwww.bxwx9.org%2Fbinfo%2F19%2F19765.htm&k=d0e9028572a1dec2f3842231a7037928&encodedQuery=&query=&w=01020400&pid=sogou-brse-1cdf14d1e3699d61&pid=AG8Og"
length: 3
/^(\w+:)\/\/([^\/]+)(\/[^?]+)/.exec(url)
0: "http://snapshot.sogoucdn.com/websnapshot"
1: "http:"
2: "snapshot.sogoucdn.com"
3: "/websnapshot"
index: 0
input: "http://snapshot.sogoucdn.com/websnapshot?&url=http%3A%2F%2Fwww.bxwx9.org%2Fbinfo%2F19%2F19765.htm&k=d0e9028572a1dec2f3842231a7037928&encodedQuery=&query=&w=01020400&pid=sogou-brse-1cdf14d1e3699d61&pid=AG8Og"
length: 4
/^(\w+:)\/\/([^\/]+)(\/[^?]+)(\?([^#]+))/.exec(url)
0: "http://snapshot.sogoucdn.com/websnapshot?&url=http%3A%2F%2Fwww.bxwx9.org%2Fbinfo%2F19%2F19765.htm&k=d0e9028572a1dec2f3842231a7037928&encodedQuery=&query=&w=01020400&pid=sogou-brse-1cdf14d1e3699d61&pid=AG8Og"
1: "http:"
2: "snapshot.sogoucdn.com"
3: "/websnapshot"
4: "?&url=http%3A%2F%2Fwww.bxwx9.org%2Fbinfo%2F19%2F19765.htm&k=d0e9028572a1dec2f3842231a7037928&encodedQuery=&query=&w=01020400&pid=sogou-brse-1cdf14d1e3699d61&pid=AG8Og"
5: "&url=http%3A%2F%2Fwww.bxwx9.org%2Fbinfo%2F19%2F19765.htm&k=d0e9028572a1dec2f3842231a7037928&encodedQuery=&query=&w=01020400&pid=sogou-brse-1cdf14d1e3699d61&pid=AG8Og"
index: 0
input: "http://snapshot.sogoucdn.com/websnapshot?&url=http%3A%2F%2Fwww.bxwx9.org%2Fbinfo%2F19%2F19765.htm&k=d0e9028572a1dec2f3842231a7037928&encodedQuery=&query=&w=01020400&pid=sogou-brse-1cdf14d1e3699d61&pid=AG8Og"
length: 6
/^(\w+:)\/\/([^\/]+)(\/[^?]+)(?:\?([^#]+))/.exec(url)
0: "http://snapshot.sogoucdn.com/websnapshot?&url=http%3A%2F%2Fwww.bxwx9.org%2Fbinfo%2F19%2F19765.htm&k=d0e9028572a1dec2f3842231a7037928&encodedQuery=&query=&w=01020400&pid=sogou-brse-1cdf14d1e3699d61&pid=AG8Og"
1: "http:"
2: "snapshot.sogoucdn.com"
3: "/websnapshot"
4: "&url=http%3A%2F%2Fwww.bxwx9.org%2Fbinfo%2F19%2F19765.htm&k=d0e9028572a1dec2f3842231a7037928&encodedQuery=&query=&w=01020400&pid=sogou-brse-1cdf14d1e3699d61&pid=AG8Og"
index: 0
input: "http://snapshot.sogoucdn.com/websnapshot?&url=http%3A%2F%2Fwww.bxwx9.org%2Fbinfo%2F19%2F19765.htm&k=d0e9028572a1dec2f3842231a7037928&encodedQuery=&query=&w=01020400&pid=sogou-brse-1cdf14d1e3699d61&pid=AG8Og"
length: 5
//网址没选好,少了 '#' 的内容
/^(\w+:)\/\/([^\/]+)(\/[^?]+)?(?:\?([^#]+))?(#.*)?$/.exec(url)
0:
"http://snapshot.sogoucdn.com/websnapshot?&url=http%3A%2F%2Fwww.bxwx9.org%2Fbinfo%2F19%2F19765.htm&k=d0e9028572a1dec2f3842231a7037928&encodedQuery=&query=&w=01020400&pid=sogou-brse-1cdf14d1e3699d61&pid=AG8Og"
1: "http:"
2: "snapshot.sogoucdn.com"
3: "/websnapshot"
4: "&url=http%3A%2F%2Fwww.bxwx9.org%2Fbinfo%2F19%2F19765.htm&k=d0e9028572a1dec2f3842231a7037928&encodedQuery=&query=&w=01020400&pid=sogou-brse-1cdf14d1e3699d61&pid=AG8Og"
5: undefined
index: 0
input: "http://snapshot.sogoucdn.com/websnapshot?&url=http%3A%2F%2Fwww.bxwx9.org%2Fbinfo%2F19%2F19765.htm&k=d0e9028572a1dec2f3842231a7037928&encodedQuery=&query=&w=01020400&pid=sogou-brse-1cdf14d1e3699d61&pid=AG8Og"
length: 6