记录一些常用的正则表达式

开发中总能遇到很多需要正则表达式的地方,这里记录一些,方便查阅。

判断html标签类

标题 head => title

1
/<TITLE>([\w\W]*?)<\/TITLE>/si

关键字 head => keywords

因为不能确定位置和符号,所以用了四个来覆盖所有情况。

1
2
3
4
/<META\s+name="keywords"\s+content="([\w\W]*?)"/si
/<META\s+name='keywords'\s+content='([\w\W]*?)'/si
/<META\s+content="([\w\W]*?)"\s+name="keywords"/si
/<META\s+http-equiv="keywords"\s+content="([\w\W]*?)"/si

描述 head => description

1
2
3
4
/<META\s+name="description"\s+content="([\w\W]*?)"/si
/<META\s+name='description'\s+content='([\w\W]*?)'/si
/<META\s+content="([\w\W]*?)"\s+name="description"/si
/<META\s+http-equiv="description"\s+content="([\w\W]*?)"/si

判断完整连接

1
/(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/

获取连接的协议加域名部分

1
2
3
4
5
/(http|https):\/\/[^\/]+\//i

// 你同样可以使用dom
window.location.protocol + '//' + window.location.host // 兼容 ie8 以上
window.location.origin // 兼容 ie 11 以上