Skip to content

Commit

Permalink
一些正则表达式随记 添加
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Nov 5, 2016
1 parent eac6b3b commit c679c49
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 1 deletion.
133 changes: 133 additions & 0 deletions Javascript/一些正则表达式随记.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@

一些正则表达式随记
---

通过一些例子来学习正则表达式摘录


## 获取url中域名、协议正则

```js
//获取url中域名、协议正则 'http://xxx.xx/xxx','https://xxx.xx/xxx','//xxx.xx/xxx'
/^(http(?:|s)\:)*\/\/([^\/]+)/.test("http://www.baidu.com");

// 必须有协议
/^[a-zA-Z]+:\/\//.test("http://www.baidu.com");
```


## 去除首尾的‘/’

```js
//去除首尾的‘/’
input = input.replace(/^\/*|\/*$/g,'');
```

## javascript:: 、javascript:void(0)

```js
href.match(/^(javascript\s*\:|#)/);
```

## 匹配

```js
var str = "access_token=dcb90862-29fb-4b03-93ff-5f0a8f546250; refresh_token=702f4815-a0ff-456c-82ce-24e4d7d619e6; account_uid=1361177947320160506170322436";
str.match(/account_uid=([^\=]+(\;)|(.*))/ig);
```

## 匹配后缀

```js
var strRegex = "(.jpg|.gif|.txt)";
var re=new RegExp(strRegex);
if (re.test(str)){

}
/(.jpg|.gif)+(\?|\#|$)/.test('a/b/c.jpgsss'); //=> false
/(.jpg|.gif)+(\?|\#|$)/.test('a/b/c.jpg?'); //=> true
```

## 匹配一些字符

```js
var str = 'asdf html-webpack-plugin for "index/index.html" asdfasdf';
str.match(/html-webpack-plugin for \"(.*)\"/ig);
console.log(RegExp.$1) //=>index/index.html
```

## 关键字符替换

```js
'css/[hash:8].index-index.css'.replace(/\[(?:(\w+):)?(contenthash|hash)(?::([a-z]+\d*))?(?::(\d+))?\]/ig,'(.*)');
//=> css/(.*).index-index.css
```

## 替换参数中的值

```js
var str = '<!DOCTYPE html><html manifest="../../cache.manifest" lang="en"><head><meta charset="UTF-8">';
str.replace(/<html[^>]*manifest="([^"]*)"[^>]*>/,function(word){
return word.replace(/manifest="([^"]*)"/,'manifest="'+url+'"');
}).replace(/<html(\s?[^\>]*\>)/,function(word){
if(word.indexOf('manifest')) return word;
return word.replace('<html','<html manifest="'+url+'"');
});
//原:<!DOCTYPE html><html manifest="../../cache.manifest" lang="en"><head><meta charset="UTF-8">
//替换成=> <!DOCTYPE html><html manifest="cache.manifest" lang="en"><head><meta charset="UTF-8">
```

## 调换

```js
var name = "Doe, John";
name.replace(/(\w+)\s*, \s*(\w+)/, "$2 $1");
//=> "John Doe"
```

## 小数点后几位验证

```js
// 精确到1位小数
/^[1-9][0-9]*$|^[1-9][0-9]*\.[0-9]$|^0\.[0-9]$/.test(1.2);

// 精确到2位小数
/^[0-9]+(.[0-9]{2})?$/.test(1.221);
```

## 校验密码强度

必须是包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间。

```js
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$/.test("weeeeeeeW2");
```

## 校验中文

```js
/^[\u4e00-\u9fa5]{0,}$/.test("但是d"); //false
/^[\u4e00-\u9fa5]{0,}$/.test("但是"); //true
```

## 由数字、26个英文字母或下划线组成的字符串

```js
/^\w+$/.test("ds2_@#"); // false
```

## 18位身份证号码校验

```js
/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test("11112319220403281X");
// true
```

## 校验日期

“yyyy-mm-dd“ 格式的日期校验,已考虑平闰年。

```js
/^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/.test("0007-12-22");
// true
```
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# 笔记/搜集
# 笔记/搜集/摘录/实践

这里放置我的笔记、搜集、摘录,给自己建设自我的知识体系,保持好奇心。

- **[50多个公众账号推荐,助你了解程序员的世界](other/公众账号推荐.md)**
- **[Git常用命令及教程网站清单](other/Git%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4%E6%B8%85%E5%8D%95.md)**
Expand Down Expand Up @@ -33,6 +35,7 @@
- Javascript
- **[思维导图来学习 Javascript 基础知识](Javascript/)**
- [轻量高效的开源JavaScript插件和库](Javascript/轻量高效的开源JavaScript插件和库.md)
- [一些正则表达式随记](Javascript/一些正则表达式随记.md)
- [web开发的框架Backbone.js手册](http://jaywcjlove.github.io/handbook/index.html)
- 测试工具
- [测试框架mocha](http://jaywcjlove.github.io/handbook/html/%E6%B5%8B%E8%AF%95%E5%B7%A5%E5%85%B7/mocha.html)
Expand Down

0 comments on commit c679c49

Please sign in to comment.