PHP preg_replace to JavaScript | BBCode

淺唱寂寞╮ 提交于 2019-12-08 03:47:28

问题


I have regex patterns in PHP

$s = preg_replace("#\[URL\=(.*)\](.*)\[\/URL\]#Ui", "<a href=\"$1\" target=\"_blank\">$2</a>", $s);
$s = preg_replace("#\[CODE\=(.*)\](.*)\[\/CODE\]#Uis", "<pre class=\"brush: $1\">$2</pre>", $s);
$s = preg_replace("#\[URL\](.*)\[\/URL\]#Ui", "<a href=\"$1\" target=\"_blank\">$1</a>", $s);
$s = preg_replace("#\[IMG\](.*)\[\/IMG\]#Ui", "<p align='center'><img src=\"$1\" border=\"0\" /></p>", $s);

How could I convert those to JavaScript?

I tried for this combination but it's not working (?)

body.replace(/\[URL\=(.*)\](.*)\[\/URL\]/g, '<a href=\"$1\" target=\"_blank\">$2</a>');

@Kolink This is working for single image

body = body.replace(/\[IMG\]([\d\D]*)\[\/IMG\]/gi, '<p align="center"><img src="$1" border="0" /></p>');

but if I add second image it all crashes becauase in this example

[IMG]http://www.someimage.com/123.jpg[/IMG]Test
[IMG]http://www.someimage.com/456.jpg[/IMG]

match will be

http://www.someimage.com/123.jpg[/IMG]Test[IMG]http://www.someimage.com/456.jpg

回答1:


You need to add i after the g to make the regex case-insensitive.

Additionally, JavaScript doesn't support a "multiline" modifier. Instead, you will need to replace . with something like [\d\D].



来源:https://stackoverflow.com/questions/11140844/php-preg-replace-to-javascript-bbcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!