forked from johnnywong233/handbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eac6b3b
commit c679c49
Showing
2 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters