Meaning of Pathname characters

937
2
06-14-2013 10:53 AM
JillMasters
New Contributor
What do each of the pathname characters mean below, from left paren until the comma, i.e. "/\/[^]+$/"

location.pathname.replace(/\/[^/]+$/, "")

Thanks,
0 Kudos
2 Replies
derekswingley1
Frequent Contributor
What a great question. The meat of this question:

/\/[^/]+$/


is a regular expression. In context, it looks at the path relative to the host and removes everything after the last slash so that an absolute path to a module can be given to the Dojo module loader. The original source for this is the Using Custom Modules with a CDN Dojo tutorial.

Here's an explanation, piece by piece

/


Start a regular expression.

\


Escape character.

/


Look for a slash.

[^/]


Negated character set to match all characters except slash.

+


Match one or more times.

$


Match at the end of a string.

/


End of regular expression.

The purpose of this regular expression is better demonstrated by example. When using custom modules, the Dojo module loader needs to know where to find them. You tell the loader about modules with dojoConfig.packages. Specifically, the location property. So...if your app is running at example.com/maps/usefulStuff.html, and your modules are in example.com/maps/extras, you can use:

location.pathname.replace(/\/[^/]+$/, '') + "/extras"


to correctly tell Dojo where to find your modules but not have to hardcode the full path to your modules.
0 Kudos
NilsBabel
Occasional Contributor II
Thanks for your explanation.  I'm having trouble setting the location for my modules.  To follow along with your example, what would the regular expression be if your modules were in:
example.com/resources/extras?

Thanks for your help
0 Kudos