regular expressions - match all anchors with optional attributes

前端 未结 1 1972
Happy的楠姐
Happy的楠姐 2021-01-12 11:19

I have a wysiwyg editor in my back end, and it is tripping up the first regular expression I wrote. This is in PHP4, using preg_replace(). I\'m capturing the UR

1条回答
  •  北恋
    北恋 (楼主)
    2021-01-12 11:42

    this should match it alright:

    /]*)href="https?:\/\/([^"]*)"(.*?)>(.*?)<\/a>/
    

    The useful thing here is the lazy match. *? it means that it'll match only as much as it absolutely needs to, as opposed to the regular match, which is greedy.

    To demonstrate, with this text:

    a b c d a b c d

    these regexes will have different results:

    /a.*c/    selects: "a b c d a b c"
    /a.*?c/   selects: "a b c"
    

    0 讨论(0)
提交回复
热议问题