Strip unknown range of characters between first "/" and second "/"

G

Guest

Could some help m figure out to strip an unknown range of characters in a
path between the first "/" and "/" found in a folder path somewhere on my
site.

eg: /catamaranco/sales/boat/1.asp
eg: /esserman/newseltter/2004/03/
eg: /yachtservices/excel/reports/

I wish to return only:

"catamaranco"
or
"esserman"
or
"yachtservices"

Thanks for advice...
Jason
 
A

Aaron [SQL Server MVP]

<%
function stripStuff(str)
stripStuff = str
if instr(str, "/") <> instrRev(str, "/") then
strs = split(str, "/")
stripStuff = strs(1)
end if
end function

response.write "<br>" & stripStuff("/catamaranco/sales/boat/1.asp")
response.write "<br>" & stripStuff("/esserman/newseltter/2004/03/")
response.write "<br>" & stripStuff("/yachtservices/excel/reports/")
%>
 
G

Guest

Many thanks - works perfectly!

Aaron said:
<%
function stripStuff(str)
stripStuff = str
if instr(str, "/") <> instrRev(str, "/") then
strs = split(str, "/")
stripStuff = strs(1)
end if
end function

response.write "<br>" & stripStuff("/catamaranco/sales/boat/1.asp")
response.write "<br>" & stripStuff("/esserman/newseltter/2004/03/")
response.write "<br>" & stripStuff("/yachtservices/excel/reports/")
%>
 
G

Guest

Umm :) sorry Aaron, I ran into an added problem after extracting the
string...how I would then also pull out the remaiing 'stuff' and stuff that
into a variable?.

Extract: "/sales/boat/1.asp"

x= "/catamaranco/sales/boat/1.asp"

As my ultimate goal is concententation:

"www". & stripStuff("/catamaranco/sales/boat/1.asp") & ".com" & "/" &
stripTrailingStuff(X)

Result: www.catamaranco.com/sales/boat/1.asp"
 
G

Guest

Ok, I did come up with my own solution...but it seems to waste far too many
lines of code...could you tell me if there is a better way to write this:

u_CurrentURL="/catamaranco/sales/boat/1.asp"

function stripParentDirectory(str)
stripParentDirectory = str
if instr(str, "/") <> instrRev(str, "/") then

strs = split(str, "/")
stripParentDirectory = strs(1)

end if
end function

ParentDir= stripParentDirectory(u_CurrentURL)

Call SubDirPath(ParentDir, u_CurrentURL)


Sub SubDirPath(ParentDir, u_CurrentURL)

strLen = len(u_CurrentURL)
response.write "<br><br>Total String Length:" & strLen &
"<br><br>"
ParDirLen=len(ParentDir) + 2 '//add slash x 2
response.write "<br><br>Parent Dir Length:" & ParDirLen &
"<br><br>"
SubPathLen= strLen - ParDirLen
response.write "<br><br>SubPath Length:" & SubPathLen &
"<br><br>"
Response.write SubPathLen

MyString= Right(u_CurrentURL,SubPathLen)
response.write "<BR><BR>http://www." & ParentDir & ".com/" &
MyString

End Sub

Result: http://www.catamaranco.com/sales/boat/1.asp
 
G

Guest

I readpated the script with an extra function which seems to work....is this
more elegant
<%
u_CurrentURL="/catamaranco/sales/boat/1.asp"


function stripParentDirectory(str)
stripParentDirectory = str
if instr(str, "/") <> instrRev(str, "/") then
strs = split(str, "/")
stripParentDirectory = strs(1)
end if
end function


Function stripSubPath(ParentDir, u_CurrentURL)

strLen = len(u_CurrentURL)
'//response.write "<br><br>Total String Length:" & strLen &
"<br><br>"
ParDirLen=len(ParentDir) + 2 '//add slash x 2
'//response.write "<br><br>Parent Dir Length:" & ParDirLen &
"<br><br>"

SubPathLen= strLen - ParDirLen
'//response.write "<br><br>SubPath Length:" & SubPathLen & "<br><br>"
'//Response.write SubPathLen

MyString= Right(u_CurrentURL,SubPathLen)
MyString ="http://www." & ParentDir & ".com/" & MyString

stripSubPath =MyString

End Function


ParentDir= stripParentDirectory(u_CurrentURL)
Response.write stripSubPath(ParentDir, u_CurrentURL)

%>
 
C

Chris Hohmann

Umm :) sorry Aaron, I ran into an added problem after extracting the
string...how I would then also pull out the remaiing 'stuff' and stuff that
into a variable?.

Extract: "/sales/boat/1.asp"

x= "/catamaranco/sales/boat/1.asp"

As my ultimate goal is concententation:

"www". & stripStuff("/catamaranco/sales/boat/1.asp") & ".com" & "/" &
stripTrailingStuff(X)

Result: www.catamaranco.com/sales/boat/1.asp"

<script language="JavaScript" runat="SERVER">
var s = "/catamaranco/sales/boat/1.asp";
Response.Write(s.replace(/^\/([^\/]*)/,"www.$1.com"));
</script>
<br>
<script language="VBScript" runat="SERVER">
Dim re : Set re = New RegExp
With re
.Pattern = "^/([^/]*)"
.Global = True
Response.Write .Replace(s,"www.$1.com")
End With
</script>
 
E

Evertjan.

Chris Hohmann wrote on 21 aug 2004 in
microsoft.public.inetserver.asp.general:
<script language="JavaScript" runat="SERVER">
var s = "/catamaranco/sales/boat/1.asp";
Response.Write(s.replace(/^\/([^\/]*)/,"www.$1.com"));
</script>
Response.write(
[case]

<br>
<script language="VBScript" runat="SERVER">
Dim re : Set re = New RegExp
With re
.Pattern = "^/([^/]*)"
.Global = True

[Global is not effective with ^ as start of string,
anyway global=true is not what you want here.]
Response.Write .Replace(s,"www.$1.com")
End With
</script>

[If the default <%%> language is jscript the order will be reversed,
so s will not be defined yet for the vbscript part, IMHO & not tested;-)]
 
C

Chris Hohmann

Evertjan. said:
Chris Hohmann wrote on 21 aug 2004 in
microsoft.public.inetserver.asp.general:
<script language="JavaScript" runat="SERVER">
var s = "/catamaranco/sales/boat/1.asp";
Response.Write(s.replace(/^\/([^\/]*)/,"www.$1.com"));
</script>

Response.write(
[case]

I'm not sure what this means.

<br>
<script language="VBScript" runat="SERVER">
Dim re : Set re = New RegExp
With re
.Pattern = "^/([^/]*)"
.Global = True

[Global is not effective with ^ as start of string,
anyway global=true is not what you want here.]
You're correct. My intention was to set Global to false. That's what I get
for sutting and pasting. However, there are cases when searching on "^"
while having global set to true is useful, ie. when multiline is turned on.
Response.Write .Replace(s,"www.$1.com")
End With
</script>

[If the default <%%> language is jscript the order will be reversed,
so s will not be defined yet for the vbscript part, IMHO & not tested;-)]

Yes, although in this particular circumstance it was more of a "one or the
other" scenario, not both.
 
E

Evertjan.

Chris Hohmann wrote on 23 aug 2004 in
microsoft.public.inetserver.asp.general:
Evertjan. said:
Chris Hohmann wrote on 23 aug 2004 in
microsoft.public.inetserver.asp.general:
<script language="JavaScript" runat="SERVER">
var s = "/catamaranco/sales/boat/1.asp";
Response.Write(s.replace(/^\/([^\/]*)/,"www.$1.com"));
</script>

Response.write(
[case]

I'm not sure what this means.

See the difference!

.write should be lower case in javascript

Response is a built-in ASP object. As such the names of its
methods/members/properties are independent of the scripting language.

I didn't know that, Chris, silly me. ;-)
 
C

Chris Hohmann

Evertjan. said:
Chris Hohmann wrote on 23 aug 2004 in
microsoft.public.inetserver.asp.general:
<script language="JavaScript" runat="SERVER">
var s = "/catamaranco/sales/boat/1.asp";
Response.Write(s.replace(/^\/([^\/]*)/,"www.$1.com"));
</script>

Response.write(
[case]

I'm not sure what this means.

See the difference!

.write should be lower case in javascript

Response is a built-in ASP object. As such the names of its
methods/members/properties are independent of the scripting language. Here's
the documentation on the Response.Write method, listing it with a capital
"W":

http://www.msdn.microsoft.com/library/en-us/iissdk/iis/ref_vbom_resomwrite.asp
 
D

Dave Anderson

Evertjan. said:
Response.write(
[case]

I'm not sure what this means.

See the difference!

.write should be lower case in javascript

I don't know about javascript, but if this suddenly became true in JScript,
I would have a lot of work to do....


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
E

Evertjan.

Dave Anderson wrote on 23 aug 2004 in
microsoft.public.inetserver.asp.general:
I don't know about javascript, but if this suddenly became true in
JScript, I would have a lot of work to do....

as I was told, Response.Write is not a part of the language, but of a
server object that is caseinsensitive, so

ResPOnse.wrITE('Hi') should be valid.

=============

However jscript in IE is not so forgiving with document.write:

<script type="text/jscript">
document.Write('Hi')
</script>

gives an error!
 
D

Dave Anderson

Evertjan. said:
as I was told, Response.Write is not a part of the language,
but of a server object that is caseinsensitive, so

ResPOnse.wrITE('Hi') should be valid.

No, that's not what you were told. The Response object does not require case
sensitivity for accessing its properties and methods, but the Response
object still must be found in the global namespace, and accordingly must be
spelled exactly "Response":

Microsoft JScript runtime error '800a1391'
'ResPOnse' is undefined


If you had spent 5 seconds testing either of your assertions, you may have
seen that for yourself.


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
E

Evertjan.

Dave Anderson wrote on 24 aug 2004 in
microsoft.public.inetserver.asp.general:
No, that's not what you were told. The Response object does not
require case sensitivity for accessing its properties and methods, but
the Response object still must be found in the global namespace, and
accordingly must be spelled exactly "Response":

Microsoft JScript runtime error '800a1391'
'ResPOnse' is undefined


If you had spent 5 seconds testing either of your assertions, you may
have seen that for yourself.

So now I am. Told that is.

You were wrong about the 5 seconds, though.
 
D

Dave Anderson

Evertjan. said:
So now I am. Told that is.

You were wrong about the 5 seconds, though.

Fair enough. I suppose the day was getting long and my nerves short. Sorry.


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top