^:匹配开头,在多行匹配中匹配行头
$:匹配结尾,在多行匹配中匹配结尾

头尾匹配

  1. const reg: RegExp = /^|$/g;
  2. const str: String = 'how\nare\nyou';
  3. console.log(str.replace(reg,'#'));
  4. // #how
  5. // are
  6. // you#

多行匹配

  1. const reg: RegExp = /^|$/gm;
  2. const str: String = 'how\nare\nyou';
  3. console.log(str.replace(reg,'#'));
  4. // #how#
  5. // #are#
  6. // #you#

\b:单词边界。\w和\W之间的位置,也包括\w到^之间位置,\w到$之间位置。
\B:
(?=p):p可以是任意字符, 匹配p前面的位置
(?!p):和上面一个意思。唯一不同的是取反

  1. // (?=p)
  2. console.log('hello'.replace(/(?=l)/g,'#')); // he#l#lo
  1. // (?!p)
  2. console.log('hello'.replace(/(?!l)/g,'#')); // #h#ell#o#