RegEx

数字加逗号

例一:1000000 -> 1,000,000

  1. 可以匹配,数字之后,三个数字之前
function addCommas(num: string): string {
  return num.replace(/(?<=\d)(?=(?:\d{3})+$)/g, ',')
}

Variable Name

  • 只包含字母、数字、下划线
  • 不能以数字开头
/[a-zA-Z_][0-9a-zA-Z_]*/

Balance

  • 100100.22
/([0-9]+)(\.[0-9]{2})?/

URL

  • shceme://hostname[:port][path][?query][#hash]
/(http|https):\/\// // shceme
/([0-9a-z-]+)(\.\2)*/ // hostname
/(:[0-9]+)?/ // port
/((\/\2)(\.\2)?)?/ // path
/((\?\2=\2)(&\2=\2)+)?/ // query
/(#\2)?/ // hash

HTML Tag

Time

  • 12-hour time (0:00 to 12:59)
/(1[0-2]|[0-9]):([0-5][0-9])/
  • 24-hour time (0:00 to 23:59)
/([0-1]?[0-9]|2[0-3]):([0-5][0-9])/