问题
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