Author |
Topic  |
|
Shaggy
Support Moderator
    
Ireland
6780 Posts |
Posted - 25 April 2007 : 07:43:31
|
The finer workings of RegExps continue to elude me and I think I'm destined to never understand their full complexities.
But, after spending a couple of hours with some tutorials and cursing a whole hell of a lot, I've finally managed to grasp the very, very basics and have thrown together my first script Yay me!
Just wanted to run it past the rest of ye to see if you think there might be any room for improvement and to ask for a bit of help, as well.function parse(string)
dim objRegex
parse=string
set objRegex=new RegExp
objRegex.ignorecase=true
objRegex.global=true
objRegex.pattern="\n\n"
parse=objRegex.replace(parse,"</p><p>")
objRegex.pattern="\n"
parse=objRegex.replace(parse,"<br />")
objRegex.pattern="\[(abbreviation)=(.*)\](.*)\[/\1\]"
parse=objRegex.replace(parse,"<abbr title=""$2"">$3</abbr>")
objRegex.pattern="\[(acronym)=(.*)\](.*)\[/\1\]"
parse=objRegex.replace(parse,"<$1 title=""$2"">$3</$1>")
objRegex.pattern="\[(bold)\](.*)\[/\1]"
parse=objRegex.replace(parse,"<strong>$2</strong>")
objRegex.pattern="\[(bullet)\](.*)\[/\1\]"
parse=objRegex.replace(parse,"<li>$2</li>")
objRegex.pattern="\[(color)=(.*)\](.*)\[/\1\]"
parse=objRegex.replace(parse,"<span style=""color:$2;"">$3</span>")
objRegex.pattern="\[(heading)\](.*)\[/\1\]"
parse=objRegex.replace(parse,"</p><h2>$2</h2><p>")
objRegex.pattern="\[(italic)\](.*)\[/\1\]"
parse=objRegex.replace(parse,"<em>$2</em>")
objRegex.pattern="\[line\]"
parse=objRegex.replace(parse,"</p><hr /><p>")
objRegex.pattern="\[(link)=(.*) title=(.*)\](.*)\[/\1\]"
parse=objRegex.replace(parse,"<a href=""$2"" title=""$3"">$4</a>")
objRegex.pattern="\[(list)\](.*)\[/\1\]"
parse=objRegex.replace(parse,"</p><ul>$2</ul><p>")
objRegex.pattern="\[(subheading)\](.*)\[/\1\]"
parse=objRegex.replace(parse,"</p><h3>$2</h3><p>")
objRegex.pattern="\[(underline)\](.*)\[/\1\]"
parse=objRegex.replace(parse,"<span class=""u"">$2</span>")
objRegex.pattern="<p></p>"
parse=objRegex.replace(parse,"")
set objRegex=nothing
end function Firstly, is \n the equivalent of VBScript's vbnewline and is there any difference between \n and \r?
Next, can anyone see any patterns and replacements I might be able to combine above? I'm thinking not as the forum code tags I'm using aren't the same as their HTML equivalents (e.g., bold & strong).
Finally, and most importantly, I need to be able to distinguish between internal and external links when replacing the [link] tag and add an onclick event to my anchor tag for external link. Something along the lines of if $2 starts with http(s):// and doesn't contain mydomain.tld. If that's not possible then I'll just have to add a new [page] tag for internal links.
|
Search is your friend “I was having a mildly paranoid day, mostly due to the fact that the mad priest lady from over the river had taken to nailing weasels to my front door again.” |
|
pdrg
Support Moderator
    
United Kingdom
2897 Posts |
Posted - 25 April 2007 : 09:16:48
|
\n and \r are different - remember typewriters? (please say yes...!) when you got to the end of a line, you pressed the lever on the right which would perform a linebreak and a carriage return? \n is the linebreak \r is the carriage return (or the other way around). It's slightly academic, but can be important in the same was that ' and ` are different ASCII values.
In VBScript the constant vbCRLF (carriage return line feed) gives ascii values (10) and (13) [maybe it's 13, then 10, I can't remember]. If you're pattern searching for ascii value of 10, 13 won't match, and vice versa. So yes, it's important, but you may get away with it a lot.
Fraid I'm not much help with the rest... |
 |
|
HuwR
Forum Admin
    
United Kingdom
20595 Posts |
Posted - 25 April 2007 : 14:09:20
|
I would recomend downloading Regular Expression Desiner by Rad Software, http://www.radsoftware.com.au I have found it a great help in testing and debugging regExp's |
 |
|
Doug G
Support Moderator
    
USA
6493 Posts |
Posted - 25 April 2007 : 19:48:30
|
quote: Firstly, is \n the equivalent of VBScript's vbnewline and is there any difference between \n and \r?
To expand a bit, your assumption is incorrect. End of line markers in windows files consist of a pair of characters, CR and LF. Unix based systems use just the LF (\n). \r is a CR code.
Here's wikipedia's take on the subject
http://en.wikipedia.org/wiki/Newline
|
====== Doug G ====== Computer history and help at www.dougscode.com |
 |
|
Shaggy
Support Moderator
    
Ireland
6780 Posts |
Posted - 26 April 2007 : 04:07:24
|
Thanks for the clarification, guys And for the link, Huw. Never had to use anything other than my text editor and browser for programming and debugging but I've been struggling that long with regular expressions, I'll give anything a try t this stage 
|
Search is your friend “I was having a mildly paranoid day, mostly due to the fact that the mad priest lady from over the river had taken to nailing weasels to my front door again.” |
 |
|
ILLHILL
Junior Member
 
Netherlands
341 Posts |
Posted - 06 May 2007 : 17:14:48
|
"if $2 starts with http(s):// and doesn't contain mydomain.tld."
Not sure if I understood you all the way, but there is an Islike function you could try to use. (I have used it to determine whether an entered url was a youtube video or a wma video and then have a flash plugin load, or a wma plugin) so maybe that might be something.
http://www.aspemporium.com/codelib.aspx?pid=39&cid=3 a little more on it, but any search for "islike asp function" will do on Google.
|
CLPPR.com - All The News Only Seconds Away |
 |
|
|
Topic  |
|
|
|