Match a given path name against a URL
npm install pathname-matcher --save
// using es modules
import pathnameMatcher from 'pathname-matcher'
// common.js
const pathnameMatcher = require('pathname-matcher')
Or use script tags and globals.
<script src="https://unpkg.com/pathname-matcher"></script>
And then grab it off the global like so:
const isPath = pathnameMatcher.default;
Let's assume you want to run a function if a user navigate to a specific path
// www.site.com/hello/search
if (pathnameMatcher(document.location.pathname, 'search')) {
console.log('Hello!!');
}
// www.site.com/search/myPage
if (pathnameMatcher(document.location.pathname, 'search/myPage')) {
console.log('Hello!!');
}
You can provide any path or url as long as it is provided as a string
Returns true
if pathname matches the given URL (no trailing slash)
OR false
when no match.
pathname should be passed without /
at the beginning of the string. See examples above.
Released under MIT license.