473,394 Members | 1,751 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Reg Exp - JScript

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

Jul 19 '05 #1
3 1484
*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[i];
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)
--
Andrew Urquhart
- FAQ: http://www.aspfaq.com
- Archive: http://tinyurl.com/38kzx (Google Groups)
- Contact me: http://andrewu.co.uk/contact/
Jul 19 '05 #2
On Wed, 08 Sep 2004 18:35:31 GMT, Andrew Urquhart
<us**************************@com.invalid> 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[i];
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.
Jul 19 '05 #3
*Harag* wrote in microsoft.public.inetserver.asp.general:
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?


I normally use " and \" exclusively and leave ' for punctuation, but
just thought the ' looked less cluttered for the example, nothing more
:-)
--
Andrew Urquhart
- FAQ: http://www.aspfaq.com
- Archive: http://tinyurl.com/38kzx (Google Groups)
- Contact me: http://andrewu.co.uk/contact/
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Harag | last post by:
Hi All. I'm stating out doing some web developing. I was wondering which of the server side languages should I concentrate on and learn. I Know CSS, HTML, T-SQL I can look at the client...
4
by: Harag | last post by:
Hi All I currently thinking of converting from my little knowledge of VBscript to jScript ASP. With this in mind I'm looking at my current code to see how it will convert over to Jscript. ...
3
by: Christopher Brandsdal | last post by:
Hi! Maby this is wring newsgroup.. I'm using ASP / VBscript on my own cms system, but I needed to use some jscript to make something work... I'm new to jscript, so here is a simple question:...
14
by: John Bentley | last post by:
Note this is crossposted to comp.lang.javacript and microsoft.public.dotnet.scripting. After some Googling and FAQing my understanding of these terms is, crudely: Javascript (3 different...
2
by: Zach Mortensen | last post by:
I can't seem to get dynamically-compiled JScript code to use C#-defined custom attributes. I have a simple attribute and a class defined in a C# assembly: namespace MyNamespace { public...
4
by: RFS666 | last post by:
Hello, I have the following problem: I have a web project where I display an activeX control that displays 2D-graphs on an aspx-page. I use jscript to access and modify the properties of the...
6
by: RFS666 | last post by:
Hello, After I posted yesterday "using C# class in jscript", I have a new problem: I have a C# class - DBResult - that contains (and other variables) a string array (and other variables), that...
7
by: RFS666 | last post by:
Hello, I would like to use variables with a type in jscript.NET. I declare them as follows: var x : double = 5.03; This doesn't work in my script, that I write to the page in codebehind with...
5
by: Maxwell2006 | last post by:
Hi, I have a requirement to develop an application component using only Server-Side Jscript (not Jscript.NET). What is Server-Side Jscript?
1
by: Andrew Wan | last post by:
How can VBScript code access JScript code variables in the same ASP page? <SCRIPT LANGAUGE="VBScript"> Dim a a = 10 </SCRIPT> <SCRIPT LANGUAGE="JScript"> Response.Write(a); </SCRIPT>
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.