Reg Exp - JScript

Discussion in 'ASP General' started by Harag, Sep 8, 2004.

  1. Harag

    Harag Guest

    Hi all

    Using JScript. is there a better way to write the following reg exp.
    or am I doing something right for a change ? lol.

    thanks
    Al


    String.prototype.convertBBCtoHTML = JS_convertBBCtoHTML;

    function JS_convertBBCtoHTML() {
    var sString = this.toString();

    sString = sString.replace(/\[b\]/gi, '<strong>');
    sString = sString.replace(/\[\/b\]/gi, '</strong>');
    sString = sString.replace(/\[strong\]/gi, '<strong>');
    sString = sString.replace(/\[\/strong\]/gi, '</strong>');
    sString = sString.replace(/\[i\]/gi, '<em>');
    sString = sString.replace(/\[\/i\]/gi, '</em>');
    sString = sString.replace(/\[em\]/gi, '<em>');
    sString = sString.replace(/\[\/em\]/gi, '</em>');
    sString = sString.replace(/\[u\]/gi, '<u>');
    sString = sString.replace(/\[\/u\]/gi, '</u>');
    sString = sString.replace(/\[s\]/gi, '<strike>');
    sString = sString.replace(/\[\/s\]/gi, '</strike>');
    sString = sString.replace(/\[pre\]/gi, '<pre>');
    sString = sString.replace(/\[\/pre\]/gi, '</pre>');
    sString = sString.replace(/\[blockquote\]/gi, '<blockquote>');
    sString = sString.replace(/\[\/blockquote\]/gi,
    '</blockquote>');

    return sString;
    }
    Harag, Sep 8, 2004
    #1
    1. Advertising

  2. Harag

    Harag Guest

    On Wed, 08 Sep 2004 18:35:31 GMT, Andrew Urquhart
    <> wrote:

    >*Harag* wrote in microsoft.public.inetserver.asp.general:
    >> Hi all
    >>
    >> Using JScript. is there a better way to write the following reg exp.
    >> or am I doing something right for a change ? lol.

    >[snip]
    >
    >Not tested, but either:
    >
    >function JS_convertBBCtoHTML() {
    > var sString = this.toString();
    > sString = sString.replace(/\[(\/?)(strong|b)\]/ig, "<$1strong>");
    > sString = sString.replace(/\[(\/?)(em|i)\]/ig, "<$1em>");
    > sString = sString.replace(/\[(\/?)u\]/ig, "<$1u>");
    > sString = sString.replace(/\[(\/?)s\]/ig, "<$1strike>");
    > sString = sString.replace(/\[(\/?)pre\]/ig, "<$1pre>");
    > sString = sString.replace(/\[(\/?)blockquote\]/ig, "<$1blockquote>");
    > return sString;
    >}
    >
    >(which could be written more succinctly as:)
    >function JS_convertBBCtoHTML() {
    > return this.toString().replace(/\[(\/?)(strong|b)\]/ig, "<$1strong>").replace(/\[(\/?)(em|i)\]/ig, "<$1em>").replace(/\[(\/?)u\]/ig, "<$1u>").replace(/\[(\/?)s\]/ig, "<$1strike>").replace(/\[(\/?)pre\]/ig, "<$1pre>").replace(/\[(\/?)blockquote\]/ig, "<$1blockquote>");
    >}
    >
    >
    >or if you're feeling more adventurous:
    >
    >function JS_convertBBCtoHTML() {
    > var sString = this.toString();
    > var tags = [['strong','b'],['em','i'],['u'],['strike','s'],['pre'],['blockquote']];
    > for (var i in tags) {
    > var item = tags;
    > var objRE = new RegExp("\\[(\\/?)(" + item.join("|") + ")\\]", "ig");
    > sString = sString.replace(objRE, "<$1" + item[0] + ">");
    > }
    > return sString;
    >}
    >
    >(Note, this last one will replace [strike] and [/strike] as well)



    Thanks Andrew.

    These different ones are much better. I especially like the last one.

    btw I was wondering, when do you decide to use single & double quotes
    in your JS ?

    I normally do all single quotes in Jscript and double in html codes so
    I can do:

    out('<table border="0" cellpadding="0">');

    Without worring about escaping them, but I've noticed in the above you
    use both kinds and was just wondering why?

    Thanks again.

    Al.
    Harag, Sep 9, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Rowland

    Reg exp: matching relative path only.

    Andrew Rowland, Aug 2, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,134
    Andrew Rowland
    Aug 2, 2003
  2. psk

    Newbie-Reg Exp

    psk, Jan 16, 2004, in forum: Perl
    Replies:
    2
    Views:
    1,351
    Gunnar Hjalmarsson
    Jan 19, 2004
  3. PerlE

    Reg Exp Help

    PerlE, Jan 30, 2004, in forum: Perl
    Replies:
    0
    Views:
    496
    PerlE
    Jan 30, 2004
  4. Ken Chesak

    reg exp

    Ken Chesak, Aug 26, 2004, in forum: Perl
    Replies:
    0
    Views:
    617
    Ken Chesak
    Aug 26, 2004
  5. Ken Chesak

    reg exp

    Ken Chesak, Aug 30, 2004, in forum: Perl
    Replies:
    5
    Views:
    640
Loading...

Share This Page