Home  >  Article  >  Backend Development  >  正则表达式 - 如何把一个PHP中递归正则的语句替换为JavaScript非递归的语句?

正则表达式 - 如何把一个PHP中递归正则的语句替换为JavaScript非递归的语句?

WBOY
WBOYOriginal
2016-06-06 20:42:59981browse

最近正在做一个js版本的markdown解析库,打算将网上比较流行的一个名为Parsedown的库完整的迁移到js上,这样前后端就有相同的展现效果了,但是我发现它的原作者在使用正则时用到了递归,但js正则又是不支持递归的,所以寻求一个解决方法能完美迁移,下面是PHP代码,注意preg_match部分

    protected function identifyLink($Excerpt)
    {
        $extent = $Excerpt['text'][0] === '!' ? 1 : 0;

        if (strpos($Excerpt['text'], ']') and preg_match('/\[((?:[^][]|(?R))*)\]/', $Excerpt['text'], $matches))
        {

回复内容:

最近正在做一个js版本的markdown解析库,打算将网上比较流行的一个名为Parsedown的库完整的迁移到js上,这样前后端就有相同的展现效果了,但是我发现它的原作者在使用正则时用到了递归,但js正则又是不支持递归的,所以寻求一个解决方法能完美迁移,下面是PHP代码,注意preg_match部分

    protected function identifyLink($Excerpt)
    {
        $extent = $Excerpt['text'][0] === '!' ? 1 : 0;

        if (strpos($Excerpt['text'], ']') and preg_match('/\[((?:[^][]|(?R))*)\]/', $Excerpt['text'], $matches))
        {

Github 官方出了个解析脚本,何必还自己去写

https://github.com/jgm/stmd

下载 js 下面那个 stmd.js 就是

看看这个,php-to-js https://github.com/Danack/PHP-to-Javascript

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn