---
# System prepended metadata

title: Regex 紀錄
tags: [技術文件, ' regex']

---

---
title: Regex 紀錄
tags: 技術文件, regex
description: 紀錄好用的function
---
# Regex MEMO

資源
- 測試
    - https://regex101.com/
- 看圖
    - https://regexper.com/

``` javascript
function test(_reg,_str){
    const regexp = RegExp(_reg,'g');
    const str = _str;
    let match = regexp.exec(str);

    if (match !== null) {
      console.log(`Found ${match[0]} start=${match.index} end=${regexp.lastIndex}.`);
    }
}

var str = "12321";
var reg = /(?<=1)2/g;// 前面是1的2
var reg = /2(?=1)/g;// 後面是1的2
var reg = /(?<!1)2/g;// 前面不是1的2
var reg = /2(?!1)/g;// 後面不是1的2

var str = "01abc10";
var reg = /1.*?\d/g;// .*?=match最少; 1後面接.*然後接數字

var reg = /(?:1)(.)/g;// (?:XXX)=不紀錄為group

test(reg,str);
```
### ex


```javascript=
// 取得cookie內容
document.cookie.match(/(?<=_ga=)[^;]*/g)
document.cookie.match(/(?<=_ga=).*?(?=;)/g)
```
