开发者

PHP Regex No Backslash

开发者 https://www.devze.com 2023-01-21 06:54 出处:网络
So I hadn\'t done any regexps for a while, so I thought I\'d brush up on my memory. I\'m trying to convert a string like a*b*c into a<b>b</b>c. I\'ve already gotten that working, but now I

So I hadn't done any regexps for a while, so I thought I'd brush up on my memory. I'm trying to convert a string like a*b*c into a<b>b</b>c. I've already gotten that working, but now I want to keep a string like a\*b\*c from turning into a\<b>b\</b>c, but rather, into a*b*c. Here's the code I'm using now:

     $string = preg_replace("/\*([\s\S]*?)\*/", "<b>$1</b>", $input);

I've tried putting this \\\\{0} in before the asterisks, and that didn't 开发者_Go百科work. Neither did [^\\\\].


Try negative lookbehind:

"/(?<!\\\\)\*([\s\S]*?)(?<!\\\\)\*/"

This only matches a * if it's not preceded by a \.

This is brittle, though; it would also fail if the string is escaped backslash \\*bold* text.

0

精彩评论

暂无评论...
验证码 换一张
取 消