【问题标题】:Regex replace text between quotes and a little more正则表达式替换引号之间的文本等等
【发布时间】:2014-08-10 05:14:32
【问题描述】:

我正在使用一个新框架,OctoberCMS,它有一个用于将页面与树枝链接的系统。这是其工作原理的示例

<a href="{{ '/about-us'|app }}">About Us</a>

我有一个巨大的表格,其中包含大量单元格内的链接,需要将链接格式更改为符合 october 的格式。

这是表格单元格中的示例链接:

<a href="/assets/Lecture%20Documents/MBCS/MBS%20Class%20Review.doc">Lecture Notes</a>

这是它必须遵循的格式

<a href=
"/assets/<...long_path_to_file...>"> </a>

<a href=
"{{ 'assets/<...long_path_to_file...>' |theme }}"> </a>

【问题讨论】:

    标签: regex regex-negation octobercms


    【解决方案1】:

    这个正则表达式应该适合你:

    匹配:

    /href=("|')(.*)("|')/ig 
    

    替换为

    'href="{{ \'$2\'|theme }}"'
    

    在 javascript 中,这看起来像:

    var a = // get anchor tag
    a.replace(/href=("|')(.*)("|')/ig, 'href="{{ \'$2\'|theme }}"');
    

    在 php 中:

    $htmlString = // get html string
    $newHtml = preg_replace('/href=("|')(.*)("|')/ig', 'href="{{ \'$2\'|theme }}"', $htmlString);
    

    【讨论】:

    • 谢谢!我最终改变了一点以满足我的要求,这是最终的正则表达式/href=([^"]*)("|')(.assets)(.*)("|')/ig
    猜你喜欢
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 2016-11-13
    • 1970-01-01
    • 2019-07-25
    • 2011-06-22
    相关资源
    最近更新 更多