Skip to content

Instantly share code, notes, and snippets.

@xuanfeng
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xuanfeng/8e4ff1fb6eab40c14ad5 to your computer and use it in GitHub Desktop.
Save xuanfeng/8e4ff1fb6eab40c14ad5 to your computer and use it in GitHub Desktop.
正则表达式记录
/* 部分替换
需求: <link href="/" rel="canonical"> 需要将href替换成data-href
解决:通过()获取某一组固定字符串,用$1表示,再进行替换
*/
var str = '<link href="/" rel="canonical"><link href="/" rel="canonical"><link data-id="" href="/" rel="canonical">';
res = str.replace(/(link.*?)href/ig, '$1data-href');
/* 身份证号
需求:检测身份证号正确性
解决:身份证号长度为15-18,最后一位可能为x或X
*/
reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
/* 域名检测
需求:检测是否为正确域名,包含端口
*/
reg = /^[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?(:\d{1,4})?$/
// HTTP支持所有端口、HTTPS不支持443端口
reg = /^https?:\/\/[\w.]+(:(?!443\b)\w+)?(\/\w*)?$/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment