^:匹配开头,在多行匹配中匹配行头
$:匹配结尾,在多行匹配中匹配结尾
头尾匹配
const reg: RegExp = /^|$/g;
const str: String = 'how\nare\nyou';
console.log(str.replace(reg,'#'));
// #how
// are
// you#
多行匹配
const reg: RegExp = /^|$/gm;
const str: String = 'how\nare\nyou';
console.log(str.replace(reg,'#'));
// #how#
// #are#
// #you#
\b:单词边界。\w和\W之间的位置,也包括\w到^之间位置,\w到$之间位置。
\B:
(?=p):p可以是任意字符, 匹配p前面的位置
。
(?!p):和上面一个意思。唯一不同的是取反
// (?=p)
console.log('hello'.replace(/(?=l)/g,'#')); // he#l#lo
// (?!p)
console.log('hello'.replace(/(?!l)/g,'#')); // #h#ell#o#