Forum Moderators: open

Message Too Old, No Replies

str.replace problem

searching for URL's

         

acemaster

12:22 am on Oct 26, 2008 (gmt 0)

10+ Year Member



I have created my own forum and am doing the bbcode by hand. I am allowing for users to preview their messages before they post them on the message board, only the following line doesn't convert. It works perfectly when processed through my PHP code, I'm guessing it has something to do with the ([^]]) part. I want to allow anything but a closing bracket, so everything after the equal sign and before a closing bracket is included as a URL. This is just for the user to preview it, so I'm not too worried about possible security vulnerabilities.


body = body.replace(/\[url\=([^]])\](.*?)\[\/url\]/g, "<a href='$1'>$2</a>");

rocknbil

4:51 pm on Oct 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
<script type="text/javascript">
function replaceTest(obj) {
if (document.getElementById) {
var txt=document.getElementById(obj).innerHTML;
alert(txt);
txt = txt.replace(/\[url=([^\]]*)\]([^\[]*)\[\/url\]/ig, '<a href="$1">$2</a>');
alert(txt);
}
}
</script>
</head>
<body>
<p><a href="#" onClick="return replaceTest('my_p');">test</a></p>
<p id="my_p">[url=http://example.com]example.com[/url]</p>
</body>
</html>

I think it might have been you forgot a couple things. A dissection:

\[url= starts with
([^\]]*) zero or more of anything not a ], store in $1
\] followed by this
([^\[]*) zero or more of anything not a [, store in $2
\[\/url\] followed by the closing BB code.

Note I also added the i flag for case-insensitive.

acemaster

5:41 pm on Oct 26, 2008 (gmt 0)

10+ Year Member



Thanks for the reply, it works perfectly.