How to split regular expression is correct

Correct regular expression split

  • Regular facilitate the development and also the many developers compare troubling thing, the beginning of the case of how the needs of small or relatively better, then the requirements are relatively Is time to write out themselves do not see what it is, even also called regular expressions to Mars

Splitting a regular block, the key to understand canonical

In the JavaScriptregular expression has the following structure:

  • Literal: matching a particular character, such as a matching character a.
  • Burst: a matching character many possibilities, such as [0-9] matches any digit.
  • Classifiers: a consecutive matching characters, such as a {1,3} 3 consecutive matching of a character appears most.
  • Anchor: matching a position, such as the start of the string ^.
  • Group: matching a whole, such as (ab) matches ab two consecutive characters.
  • Branch: matching one or more expressions, such as ab | ab or bc bc matching characters.

1. Points to note

  • Matching whole string

Can not be written / ^ abc | BCD \ (/, but to write / ^ (abc | BCD) \) /.

  • Quantifier put together question
    need to match: Each character is a / b / c in one, and the string length is a multiple of 3:

Can not be written / ^ [abc] {3} + $ /, but to write / ([abc] {3}) + /.

  • Meta character escaping problems

Metacharacters regular special characters, when you need to escape metacharacters match, such as:

^、$、.、*、+、?、|、、/、(、)、[、]、{、}、=、!、:、- 。

// "[abc]"  => /\[abc\]/ 或者 /\[abc]/ 
// "{1,3}"  => /\{1\}/ 或者 /\{1}/ 因为不构成字符组

2. Case Study

  • identification number
/^(\d{15}|\d{17})[\dxX]$/.test("390999199999999999");// true
  • IPV4
let r = /^((0{0,2}\d|0?\d{2}|1\d{2}|2[0-4]\d|25[0-5])\.){3}(0{0,2}\d|0?\d{2}|1\d{2}|2[0-4]\d|25[0-5])$ // true

Guess you like

Origin www.cnblogs.com/sunhang32/p/11888325.html