<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>正则表达式 on 施国鹏 (seth-shi)</title><link>https://sethix.com/zh-cn/tags/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/</link><description>Recent content in 正则表达式 on 施国鹏 (seth-shi)</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Sun, 19 Jan 2020 14:52:43 +0800</lastBuildDate><atom:link href="https://sethix.com/zh-cn/tags/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/index.xml" rel="self" type="application/rss+xml"/><item><title>使用正则的先行断言和后行断言来排版你的文字</title><link>https://sethix.com/zh-cn/posts/2020011914/</link><pubDate>Sun, 19 Jan 2020 14:52:43 +0800</pubDate><guid>https://sethix.com/zh-cn/posts/2020011914/</guid><description>如我有以下一串字符：
let str = &amp;#39;为中华\n\n只崛aaa起\n而读书\n 我是123中国1111人&amp;#39;; 想要的效果 每个换行之后自动缩进，等于添加四个空格 所有数字和其它字符的间隔有空格 所有字母和其它字符的间隔有空格 重复排版不会导致排班错乱 代码如下：
// 先行断言后面不会出现 4 个空格彩条混 const brReg = /\n(?!\s{4})/g; // 后行断言前面没有空格和数字才替换， 后面的想通 const numPreReg = /(?&amp;lt;!\s|\d)(\d+)/g; const numExtReg = /(\d+)(?!\s|\d)/g; const charPreReg = /(?&amp;lt;!\s|[a-zA-Z])([a-zA-Z]+)/g; const charExtReg = /([a-zA-Z]+)(?!\s|[a-zA-Z])/g; str = str.replace(brReg, &amp;#39;\n &amp;#39;); str = str.replace(charPreReg, &amp;#39; $1&amp;#39;); str = str.replace(charExtReg, &amp;#39;$1 &amp;#39;); str = str.replace(numPreReg, &amp;#39; $1&amp;#39;); str = str.replace(numExtReg, &amp;#39;$1 &amp;#39;); // 为中华\n \n 只崛 aaa 起\n 而读书\n 我是 123 中国 1111 人 console.</description></item></channel></rss>