Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegExp Custom markup 1

Status
Not open for further replies.

guitardave78

Programmer
Sep 5, 2001
1,294
GB
Hi All
I have the following that will take [yarslink]LINK[/yarslink]
and turn it to
<a href="LINK">LINK</a>

Code:
oRegExp.Pattern = "\[yarslink\]((.|\n)*?)\[\/yarslink\]"
str = oRegExp.Replace(str, "<a href=""$1"" target=""_blank"">$1</a>")
I would like the option to also do it this way

[yarslink url="URL"]LINK[/yarslink]
<a href="URL">LINK</a>

Any takers?

}...the bane of my life!
 
[tt]oRegExp.Pattern = "\[yarslink\s+url\s*=\s*""((.|\n)*?)""\]((.|\n)*?)\[/yarslink\]" 'no need to escape /
str = oRegExp.Replace(str, "<a href=""$1"">$3</a>")[/tt]
 
could i use this

oRegExp.Pattern = "\[yarslink\s+url\s*=\s*""((.|\n)*?)""\]((.|\n)*?)\[/yarslink\]" 'no need to escape /
Set Matches = oRegExp.Execute(str)
if Matches.Count > 0 then
str = oRegExp.Replace(str, "<a href=""$1"" target=""_blank"">$3</a>")
else
oRegExp.Pattern = "\[yarslink\]((.|\n)*?)\[\/yarslink\]"
str = oRegExp.Replace(str, "<a href=""$1"" target=""_blank"">$1</a>")
end if

}...the bane of my life!
 
Ah ok

oRegExp.Pattern = "\[yarslink\s+url\s*=\s*""((.|\n)*?)""\]((.|\n)*?)\[/yarslink\]"
str = oRegExp.Replace(str, "<a href=""$1"" target=""_blank"">$3</a>")
oRegExp.Pattern = "\[yarslink\]((.|\n)*?)\[\/yarslink\]"
str = oRegExp.Replace(str, "<a href=""$1"" target=""_blank"">$1</a>")

}...the bane of my life!
 
[tt]if oRegExp.Replace(str, "$1") <> "" then
str = oRegExp.Replace(str, "<a href=""$1"">$3</a>")
else
str = oRegExp.Replace(str, "<a href=""$3"">$3</a>")
end if[/tt]
 
No that doesnt work

[yarslink url="downloads/WIC_Trailer.wmv"]Trailer (wmv)[/yarslink]

[yarslink]http://www.worldinconflict.com[/yarslink]

returns

<a href="downloads/WIC_Trailer.wmv">Trailer (wmv)</a>

[yarslink]http://www.worldinconflict.com[/yarslink]

}...the bane of my life!
 
Oh sorry, no if there is no link text it will be just
[yarslink]asdasd[/yarslink]
This is to make it backwards compatible :(
Sorry

oRegExp.Pattern = "\[yarslink\s+url\s*=\s*""((.|\n)*?)""\]((.|\n)*?)\[/yarslink\]"
str = oRegExp.Replace(str, "<a href=""$1"" target=""_blank"">$3</a>")
oRegExp.Pattern = "\[yarslink\]((.|\n)*?)\[\/yarslink\]"
str = oRegExp.Replace(str, "<a href=""$1"" target=""_blank"">$1</a>")

Works, but is resource wasting. Any more ideas, We are close :D

}...the bane of my life!
 
Put one more paranthesis.
[tt]
oRegExp.Pattern = "\[yarslink[blue]\b(\s+url\s*=\s*""((.|\n)*?)"")?\][/blue]((.|\n)*?)\[/yarslink\]"
if oRegExp.Replace(str, "$2") <> "" then
str = oRegExp.Replace(str, "<a href=""$2"">$4</a>")
else
str = oRegExp.Replace(str, "<a href=""$4"">$4</a>")
end if
[/tt]
 
>That leaves everything unchanged ?
Not sure I understand.

Whenever submatches like "$1" etc... are to be used, the pre-conditional is always having the match to suceed to begin with. Hence, a test on the existence is in order.
[tt]
oRegExp.Pattern = "\[yarslink\b(\s+url\s*=\s*""((.|\n)*?)"")?\]((.|\n)*?)\[/yarslink\]"
if oRegExp.test(str) then
if oRegExp.Replace(str, "$2") <> "" then
str = oRegExp.Replace(str, "<a href=""$2"">$4</a>")
else
str = oRegExp.Replace(str, "<a href=""$4"">$4</a>")
end if
else
'whatever you want to deal with str
end if
[/tt]
There's a bit of a patchwork there for the pattern to take into consideration of changing requirement, hence, there could well be some detail to tidy up. But, the main ingredient should stand up to the test.
 
I've no problem in verifying the correct outcomes with it. Have you set .ignorecase=true? just in case you've URL="URL" for instance.
 
Got it
Code:
	oRegExp.Pattern = "\[yarslink(?:|.*?url=\""(.*?)\"")\](.*?)\[/yarslink\]"
	set matches=oRegExp.execute(str)
	if matches.count > 0 then
		for each match in matches
			if match.SubMatches(0) <> "" then
				str = oRegExp.Replace(str, "<a href=""$1"">$2</a>")
			else
				str = oRegExp.Replace(str, "<a href=""$2"">$2</a>")
			end if
		next
	end if

}...the bane of my life!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top