Have no idea why the first one becomes 404, so post it again.
Creating proper regex is pretty simple.
step 1: open your preferred editor
step 2: let a cat play on your keyboard
Stop joking, here comes some notes and something I may collect from Stackflow, youtube, books or somewhere I googled.
One example:
1 | ^(\d{10}|\d{12}|\d{6}-\d{4}|\d{8}-\d{4}|\d{8} \d{4}|\d{6} \d{4}) swedish id number (9901189876) |
Some very basic stuff
1 | ^: anchor to the beginning of the string |
JavaScript regex quick reference:
Seriously, I doubt if there is anyone can read and remember all this stuff in the beginning. The best way is trying to rememeber some simple basic ones. So in practice when you need to do some specific catch, repeatedly search these methods and by that you can really remember this magic stuff.
1 | [abx-z] One character of: a, b, or the range x-z |
Several good website to do the regex pattern check
1. regexr Recommended!🎇🎇🎇
2. regextester
3. regex101
4. regexper
5. for PHP
6. Too sophisticated for now though very good for sure
7. some cheatsheet
8. One article about group capture in Chinese
9. RegexEgg - good website
10. Some article about greedy mode
Lazy mode & Greedy search
Laziness is only enabled for the quantifier with ?.
Other quantifiers remain greedy.
For instance:
1 | alert( "123 456".match(/\d+ \d+?/) ); // 123 4 |
The pattern \d+
tries to match as many digits as it can (greedy mode), so it finds 123 and stops, because the next character is a space ' '.
Then there's a space in the pattern, it matches.
Then there's \d+?
. The quantifier is in lazy mode, so it finds one digit 4 and tries to check if the rest of the pattern matches from there.
…But there's nothing in the pattern after \d+?
.
The lazy mode doesn't repeat anything without a need. The pattern finished, so we're done. We have a match 123 4
.
1 | /^[a-zA-ZàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųūÿýżźñçčšžÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ∂ð ,.'-]+$/ |
⬆ for all possible alphabet in name ⬆
One article from Juejin
1.) 模式匹配的用法(x) pattern matching
也称为串匹配(string matching, pattern matching),就是给定一组特定的字符串集合P,对于任意的一个字符串T,找出P中的字符串在T中的所有出现位置。我们称P为模式串集合,称P中的元素为模式串(或关键词),称T为文本。字符串中的字符都取自一个有限的符号集合Σ ,简称字母表或字符集。
2.) 非捕获括号的模式匹配(?:x)
3.) 先行断言x(?=y)
4.) 后行断言(?<=y)x
5.) 正向否定查找x(?!y)
6.) 反向否定查找(?<!y)x
7.) 字符集合和反向字符集合的用法xyz/^xyz
8.) 词边界和非单词边界匹配/b/B
9.) 空白字符/非空白字符匹配/s/S
10.) 单字字符/非单字字符匹配/w/W
1.) Capturing Groups
模式匹配主要用来匹配某一类字符串并记住匹配项,JS中不支持命名捕获组,一般用于替换和匹配。(test/replace)
1 | let str = 'xuxi is xuxi is' |
( )=>捕获括号(Capture Groups)
2.) Non-Capturing Groups(?:x)
主要用来匹配某一类字符串但不记住匹配项。用来判断某类字符是否存在于某字符串中。(test if exist)
e.g.
1 | let str = 'xuxixuxi' |
正则表达式的先行断言和后行断言一共有4种形式:
(?=pattern) 零宽正向先行断言(zero-width positive lookahead assertion)
(?!pattern) 零宽负向先行断言(zero-width negative lookahead assertion)
(?<!pattern) 零宽负向后行断言(zero-width negative lookbehind assertion)
(?<=pattern) 零宽正向后行断言(zero-width positive lookbehind assertion)
3.) positive lookahead assertion: x(?=y)
先行断言: 匹配’x’仅仅当’x’后面跟着’y’
e.g.
1 | let str = '王者融化' |
4.) Positive Lookbehind Assertion: (?<=y)x
后行断言: 匹配’x’仅当’x’前面是’y’.
e.g.
1 | let str = 'xuxiA' |
5.) Negative Lookahead Assertion: x(?!y)
正向否定查找: 仅仅当’x’后面不跟着’y’时匹配’x’.
e.g.
1 | let str = '3.1415' |
6.) Negative Lookbehind Assertion: (?<!y)x
反向否定查找: 仅仅当’x’前面不是’y’时匹配’x’.
e.g.
1 | let str = '3.1415' |
7.) 字符集合和反向字符集合的用法 xyz / ^xyz
e.g.
1 | let str = 'abcd' |
8.) Word Boundaries & Non-Word Boundaries (\b \B)
\b 匹配一个词的边界。一个词的边界就是一个词不被另外一个“字”字符跟随的位置或者前面跟其他“字”字符的位置,例如在字母和空格之间。注意,匹配中不包括匹配的字边界。换句话说,一个匹配的词的边界的内容的长度是0。
\B 匹配一个非单词边界。匹配如下几种情况:
(1)字符串第一个字符为非“字”字符
(2)字符串最后一个字符为非“字”字符
(3)两个单词字符之间
(4)两个非单词字符之间
(5)空字符串
e.g.
1 | let str = 'xuxi' |
(1)中匹配到了单词边界,即xi, 为该字符串的末尾.(2)中应为xu为非单词边界,所以会被其匹配到.
9.) Whitespace & Non-Whitespace (\s \S)
\s: 匹配一个空白字符,包括空格、制表符、换页符和换行符.
\S: 匹配一个非空白字符
e.g.
1 | let str = 'xuxi is' |
(1)和(2)中执行之后都将匹配xuxi, 一个是空白字符之前的匹配, 一个是非空白字符的匹配.
10.) Word Character / Non-Word Character (\w /W)
\w: 匹配一个单字字符(字母、数字或者下划线)。等价于 [A-Za-z0-9_]。
\W: 匹配一个非单字字符。等价于 [^A-Za-z0-9_]
10 Examples:
1.) 去除字符串内指定元素的标签
1 | function trimTag(tagName, htmlStr) { |
2.) 短横线命名转驼峰命名
1 | // 短横线转驼峰命名, flag = 0为小驼峰, 1为大驼峰 |
3.) 实现一个简单的模板引擎
关于实现一个模板引擎, 实现中用到了大量的正则,建议感兴趣的可以直接看实现一个简单的模板引擎.
4.) 去除字符串中的空格符
1 | function trimAll(str) { |
5.) 判断指定格式的数据输入合法性
1 | function numCheck(str, specialNum) { |
6.) 去除url参数字符串中值为空的字段
1 | const trimParmas = (parmaStr:string = '') => { |
7.) 将浏览器参数字符串转化为参数对象
1 | function unParams(params = '?a=1&b=2&c=3') { |
8.) 计算字符串字节数
1 | /** |
9.) 匹配是否包含中文字符
1 | function hasCn(str) { |
10.) 实现搜索联想功能
1 | function searchLink(keyword) { |