Featured image of post Using Lookahead and Lookbehind Assertions in Regular Expressions to Format Your Text

Using Lookahead and Lookbehind Assertions in Regular Expressions to Format Your Text

Deep Dive into Lookahead and Lookbehind Assertions

Given the following string:

let str = '为中华\n\n只崛aaa起\n而读书\n     我是123中国1111人';

Desired Formatting Effects

  • Auto-indent with 4 spaces after each line break
  • Add spacing between numbers and other characters
  • Add spacing between letters and other characters
  • Idempotent formatting (repeated formatting won’t cause inconsistencies)

Implementation code:

// Lookahead assertion to ensure no 4 spaces follow
const brReg = /\n(?!\s{4})/g;
// Lookbehind assertion to check preceding characters
const numPreReg = /(?<!\s|\d)(\d+)/g;
const numExtReg = /(\d+)(?!\s|\d)/g;
const charPreReg = /(?<!\s|[a-zA-Z])([a-zA-Z]+)/g;
const charExtReg = /([a-zA-Z]+)(?!\s|[a-zA-Z])/g;

str = str.replace(brReg, '\n    ');
str = str.replace(charPreReg, ' $1');
str = str.replace(charExtReg, '$1 ');
str = str.replace(numPreReg, ' $1');
str = str.replace(numExtReg, '$1 ');

// Output: 为中华\n    \n    只崛 aaa 起\n    而读书\n     我是 123 中国 1111 人
console.log(JSON.stringify(str));

PS: This formatting logic powers YouMengJi's automatic text formatting feature