How To Get The Value After The Hash In JavaScript

Fortunately, there are functions that are able to get the value after the Hash in JavaScript. We will help you to do this using three different functions. Each way is somehow easy, so feel free to decide between options.

Get the value after the Hash in JavaScript

Using substring() and lastIndexOf()

The lastIndexOf() function can help you find the position of the last hash character appearing in a string. Its syntax and usage is defined here. The substring() method returns a specific part of a given string between the two indexes provided. Its method’s syntax can be found in this page.

let string = 'This is Learn#Share#IT'

// Get the last position of the # character
let index = string.lastIndexOf('#')

// Get the substring from that position to the end of the string
let value = string.substring(index)

console.log (value)

Output: 

#IT

The logic behind this solution is very simple, we use the lastIndexOf() method to get the last index of the delimiter (in this case is the hash ‘#’). After that, we use the substring() function which creates a string which is a part of the original string from the given index to the end of that string.

Using window.location.hash

In many cases, the string which contains the hash character you want to get the value after, comes from the website url, which represents in your browser url bar. This url can be taken to your code with the window.location.hash. For example, go to this website and run the following command:

console.log(window.location.hash)

Output: 

#Using_substr

This solution also works for the url whose more than one hash, such as this website:

The fact is, the first solution will give you the value after that last hash, and this second solution will return the value after the first hash and it will not treat other hash characters after it as a hash, so it will return like the above example. In the next solution, we will treat those hash as a separator and get only the value after the specific hash you want.

Using split() method

The split() function will help you split your string into an array of substring split by the specific delimiter.

split(delimiter)

Parameter:

  • delimiter: The delimiter character you want to split between substrings.
let string = 'This is Learn#Share#IT'

// Split substrings by the # character
let array = string.split('#')

console.log (array[1])
console.log (array[2])

Output: 

Share
IT

This solution is what you are looking for if your string has multiple hashes instead of one. Because we want to get the value after the hash, it must be the second element in the array instead of the one at the 0th index (which is the value before the hash). This solution will automatically remove the leading hash in the value you want to get. Therefore, all you have to do is get the n index’s element in the array corresponding to the order of the hash which you want to get value after it (e.g. if you want to get the value after the 2nd hash, then reference array[2]).

Summary

We have found out how to get the value after the Hash in JavaScript using three different ways. Although there are many methods to cope with this type of problem, we hope you can choose the quickest way which suits you best. Good luck for you!

Leave a Reply

Your email address will not be published. Required fields are marked *