# 一行代码把 [[xxx]] 替换为 [xxx](xxx)格式

- Link: https://www.ixiqin.com/2020/05/21/one-line-of-code-to-the-xxx-for-xxx-xxx-format/
- Published: 2020-05-21T20:57:54+08:00
- Author: 白宦成

文章目录

 [Toggle](https://www.ixiqin.com/2020/05/21/one-line-of-code-to-the-xxx-for-xxx-xxx-format/#)

 * [替换代码](https://www.ixiqin.com/2020/05/21/one-line-of-code-to-the-xxx-for-xxx-xxx-format/#%e6%9b%bf%e6%8d%a2%e4%bb%a3%e7%a0%81)
 * [一些好用的工具](https://www.ixiqin.com/2020/05/21/one-line-of-code-to-the-xxx-for-xxx-xxx-format/#%e4%b8%80%e4%ba%9b%e5%a5%bd%e7%94%a8%e7%9a%84%e5%b7%a5%e5%85%b7)
 * [为什么没有用 -i Flag?](https://www.ixiqin.com/2020/05/21/one-line-of-code-to-the-xxx-for-xxx-xxx-format/#%e4%b8%ba%e4%bb%80%e4%b9%88%e6%b2%a1%e6%9c%89%e7%94%a8_-i_flag)
       * [change log](https://www.ixiqin.com/2020/05/21/one-line-of-code-to-the-xxx-for-xxx-xxx-format/#change_log)

`[[xxx]]` 是目前比较常见的 backlink ，特别是各种笔记应用。

在转化格式的时候，如果你需要将 `[[xxx]]` 替换为传统的 markdown 格式，这个时候你可以
借助一个简单的 sed 命令来完成这些工作

## 替换代码

使用时把 test.md 替换为源文件,output.md 替换为导出的文件

    ```javascript
    sed -E "s/\[{2}(.*)\]{2}/[\\1](\\1)/g" test.md >> output.md
    Code language: JavaScript (javascript)
    ```

这段代码使用了 sed 来完成修改，其中使用正则表达式替换 backlink 。

> 说起正则表达式，在我高中的时候，我误以为正则表达式是一种数学表达式，还跑去问了
> 问我的高中数学老师，老师理所当然的不知道。

## 一些好用的工具

我自己在测试正则表达式的时候，会用到的工具主要是 [RegExr](https://regexr.com/) ，
不过最近发现有人部署了一个速度更快的 [RegExr-CN](https://regexr-cn.com/)，有需要
的同学可以试试看

## 为什么没有用 -i Flag?

因为 -i 的话，怕把文件搞坏而没有备份，用管道虽然麻烦了点，但至少不修改源文件，安全
一些。

---

#### change log

2020.05.24 fix some layout error
