[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 578
  • Last Modified:

RegExp and Replace On Load

I have a bulletin board system that I use Regular Expressions (in .NET) to prevent use of HTML and allow for certain BBCode tags to be used in place of HTML.  I want to have a preview pane that they click a button and a new window opens with a preview of what their post would look like.  The only problem is that I can't get my javascript RegExp's to work properly.  I need help in debugging this to fire with the onload attribute of the preview page.

Code:
<script language="javascript" type="text/javascript">
      function myPreview() {
            var vbCrLf = Chr(13) + Chr(10);
            var myHTML = window.opener.document.getElementById("Comment").value;
            myHTML = myHTML.replace(vbCrLf, "<br>");
            var re1 = new RegExp(<\/?[^>]*>, "g");
            if (myHTML.match(re1)) {
                  myHTML = myHTML.replace(re1, "");
            }
            var re2 = new RegExp(\[(\/?[a-zA-Z]?([0-9]?))\], "g");
            if (myHTML.match(re2)) {
                  myHTML = myHTML.replace(re2, "<$1>");
            }
            var re3 = new RegExp(\[url\=((.|\n)*?)\]((.|\n)*?)\[/url\], "g");
            if (myHTML.match(re3)) {
                  myHTML = myHTML.replace(re3, "<a href=$1 target=_blank>$3</a>");
            }
            var re4 = new RegExp(\[url\]((.|\n)*?)\[/url\], "g");
            if (myHTML.match(re4)) {
                  myHTML = myHTML.replace(re4, "<a href=$1 target=_blank>$1</a>");
            }
            document.getElementById("divPreview").innerHTML = myHTML;
      }
</script>
</head>
<body onload="myPreview();">

I know that the actual RegExp's work as they should, it's just that I can't get the code to replace them when the page loads.  What do I do?
0
jpontani
Asked:
jpontani
  • 2
1 Solution
 
ZvonkoSystems architectCommented:
What error do you get?
Is the value from "Comment" unchanged or do you get no value?

0
 
mr_egyptianCommented:
I think you're getting your VB and JS mixed a bit. This seems to work:


<html>
 <head>
  <title></title>
<script language="javascript" type="text/javascript">
     function myPreview() {
          var vbCrLf = String.fromCharCode(13) + String.fromCharCode(10);
          var myHTML = window.opener.document.getElementById("Comment").value;
          myHTML = myHTML.replace(vbCrLf, "<br>");
          var re1 = new RegExp(/<\/?[^>]*>/);
          if (myHTML.match(re1)) {
               myHTML = myHTML.replace(re1, "");
          }
          var re2 = new RegExp(/\[(\/?[a-zA-Z]?([0-9]?))\]/);
          if (myHTML.match(re2)) {
               myHTML = myHTML.replace(re2, "<$1>");
          }
          var re3 = new RegExp(/\[url\=((.|\n)*?)\]((.|\n)*?)\[\/url\]/);
          if (myHTML.match(re3)) {
               myHTML = myHTML.replace(re3, "<a href=$1 target=_blank>$3</a>");
          }
          var re4 = new RegExp(/\[url\]((.|\n)*?)\[\/url\]/);
          if (myHTML.match(re4)) {
               myHTML = myHTML.replace(re4, "<a href=$1 target=_blank>$1</a>");
          }
          document.getElementById("divPreview").innerHTML = myHTML;
     }
</script>
</head>
<body onload="myPreview();">
<div name="divPreview" id="divPreview"></div>
</body>
</html>


You need to delimit your expressions, and the escape any instance of the delimiter. There is no 'chr' datatype in JS. Hope that helps.
0
 
mr_egyptianCommented:
BTW

It may be a better design to modify your asp page to create the preview. The code is already there anyway.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now