<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Regular Expressions on seth-shi</title><link>https://sethix.com/tags/regular-expressions/</link><description>Recent content in Regular Expressions on seth-shi</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Sun, 19 Jan 2020 14:52:43 +0800</lastBuildDate><atom:link href="https://sethix.com/tags/regular-expressions/index.xml" rel="self" type="application/rss+xml"/><item><title>Using Lookahead and Lookbehind Assertions in Regular Expressions to Format Your Text</title><link>https://sethix.com/posts/2020011914/</link><pubDate>Sun, 19 Jan 2020 14:52:43 +0800</pubDate><guid>https://sethix.com/posts/2020011914/</guid><description>Given the following string:
let str = &amp;#39;为中华\n\n只崛aaa起\n而读书\n 我是123中国1111人&amp;#39;; 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&amp;rsquo;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 = /(?&amp;lt;!\s|\d)(\d+)/g; const numExtReg = /(\d+)(?</description></item></channel></rss>