前端常用正则

  1. /\B(?=(\d{3})+(?!\d))/g
    这个正则用来千分位整数
'11111100'.replace(/\B(?=(\d{3})+(?!\d))/g, ',') // "11,111,100"

其中额\B可以参考这个,写的比较详细

  1. 匹配字符串中的第一个遇到的字符
    a. 使用^,这个如果在[]中代表不匹配该字符,如果使用匹配url中的第一个/就可以这么些
'http://baidu.com/a/b/c'.match('^http\:\/\/[^\/]*\/')
// ==> ["http://baidu.com/", index: 0, input: "http://baidu.com/a/b/c", groups: undefined]

b. 使用非贪婪模式,参考# 正则表达式 - 贪婪与非贪婪(惰性)

'http://baidu.com/a/b/c'.match('^http\:\/\/.+?\/')
// ==> ["http://baidu.com/", index: 0, input: "http://baidu.com/a/b/c", groups: undefined]

你可能感兴趣的:(前端常用正则)