Reg Exp - JScript

H

Harag

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;
}
 
H

Harag

*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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top