@wordpress/url
Version:
WordPress URL utilities.
20 lines (19 loc) • 532 B
JavaScript
/**
* Returns the fragment part of the URL.
*
* @param {string} url The full URL
*
* @example
* ```js
* const fragment1 = getFragment( 'http://localhost:8080/this/is/a/test?query=true#fragment' ); // '#fragment'
* const fragment2 = getFragment( 'https://wordpress.org#another-fragment?query=true' ); // '#another-fragment'
* ```
*
* @return {string|void} The fragment part of the URL.
*/
export function getFragment( url ) {
const matches = /^\S+?(#[^\s\?]*)/.exec( url );
if ( matches ) {
return matches[ 1 ];
}
}