Regular Expressions Notes

Modifier (g, i, m)

  1. g: full match (without the first match to the stop)

  2. i: Ignore case (default case-sensitive)

  3. m: multiline search

“He is a boy,Is he?”.replace('/\bis\b/gi','0')
=>  "He 0 a boy,0 he?"

Metacharacters (*,, [], (), {}, +,, $, ^, |.? ,,)

  1. [] Character class, one can meet
 [abc] 字符类,(表或,有其中一个即可(a,b,c))
 [^abc] 字符类取反,none of  (a,b,c) 不包含组合里任意一个
 [a-z] 从a至z的任意字符
 [0-9] 从0至9的任意数字
 [a-zA-Z] 从a至z或A至Z的任意字符
  1. All symbols except the carriage return and linefeed
  2. \ D numeric characters ([0-9])
  3. \ D non-numeric characters ([^ 0-9])
  4. \ S whitespace
  5. \ S non-whitespace
  6. \ W word character (alphanumeric underscore [a-zA-Z_0-9])
  7. \ W non-word character
  8. {4} quantifier, expressed 4
  9. () Grouping
  10. Represents one or more + (\ + D 1 represents one or more numbers)
  11. ? Represents 0 or 1 (\ d? Represents 0 or a number)
2020-05-06 替换为05-06-2020
var reg = /^(\d{4})[/-](\d{2})[/-](\d{2})$/g
"2020-05-06".replace(reg,'$2-$3-$1')

Boundary (^, $, \ b, \ B)

  1. ^ XXX to start (pre-write)
  2. $ XXX to the end (after writing)
  3. \ b word boundary
  4. \ B non-word boundary
Published 68 original articles · won praise 55 · Views 400,000 +

Guess you like

Origin blog.csdn.net/Wu_shuxuan/article/details/104914344