正則學習小結(1)-基礎

定義:正則表達式(regular expression)描述了一種字符串匹配的模式(pattern)

- 正則建立方法
  1. 直接量語法 => /pattern/attributes
  2. 建立 RegExp 對象的語法 => new RegExp(pattern, attributes)
pattern(pætɚn 模式/表達式) 指的是那堆有各類符號組成的
attributes('ætrə,bjʊt 屬性/修飾符) 就三個選項 i(ignore case 忽略大小寫|執行對大小寫不敏感的匹配) g(global 執行全局匹配,查找全部匹配而非在找到第一個匹配後中止) m(mutiple容許多行匹配 不太清楚
- 方括號(方括號用於查找某個範圍內的字符,括號內的就是範圍,例如[abc], [a-z], [0-9])
var aa = 'abcdefghigklmnopqrstuvwxyz'

var result = {
   '/[^ade]/g': aa.match(/[^ade]/g),   //["b", "c", "f", "g", "h", "i", "g", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
   '/[^ade][bcd]/g': aa.match(/[^ade][bcd]/g),   //["bc"]
   '/[^ade]|[bcd]/g': aa.match(/[^ade]|[bcd]/g),   //["b", "c", "d", "f", "g", "h", "i", "g", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
   '/[^ade]&[bcd]/g': aa.match(/[^ade]&[bcd]/g),   //null
   '/(ab)/g': aa.match(/(ab)/g),   //["ab"]
   '/(ad)/g': aa.match(/(ad)/g),   //null
   '/(ab)./g': aa.match(/(ab)./g),   //["abc"]
   '/(ab).../g': aa.match(/(ab).../g),   //["abcde"]
   '/(ab.).../g': aa.match(/(ab.).../g),   //["abcdef"]
   '/(ab...).../g': aa.match(/(ab...).../g),   //["abcdefgh"]
   '/(a|b|c)/g': aa.match(/(a|b|c)/g),   //["a", "b", "c"]
   '/(a|b|c)./g': aa.match(/(a|b|c)./g),   //["ab", "cd"]
   '/(a|b|c.)./g': aa.match(/(a|b|c.)./g),   //["ab", "cde"]
   '/(a|b.|c.)./g': aa.match(/(a|b.|c.)./g),   //["ab", "cde"]
   '/(a|b.|e.)./g': aa.match(/(a|b.|e.)./g),   //["ab", "efg"]
};

 

script標籤替換html

html = html.replace(/<script.*?\/lang\/.*?></ig, matchStr => {
let result = matchStr.replace(/\.js.*?\"/ig, `.js?stamp=${stamp}"`);
return result;
})
 

1、校驗數字的表達式
/^0?0?86\d{11}/g; // 以0086,086,86開頭, 外加11位數字的字符串正則表達式

相關文章
相關標籤/搜索