Thursday, June 6, 2024
 Popular · Latest · Hot · Upcoming
38
rated 0 times [  39] [ 1]  / answers: 1 / hits: 15383  / 8 Years ago, wed, june 1, 2016, 12:00:00

How can I check whether the page url contains a '#' character plus some random digits




e.g. www.google.de/#1234




if( window.location.href.indexOf('#') > 0 ){
alert('true');
}


Does indexOf support Regular Expressions?


More From » regex

 Answers
3

Use String.prototype.search to get the index of a regex:



'https://example.com/#1234'.search(/#d+$/); // 20


And RegExp.prototype.test if used for boolean checks:



/#d+$/.test('https://example.com/#1234'); // true


The regex used for these examples are /#d+$/ which will match literal # followed by 1 or more digits at the end of the string.



As pointed out in the comments you might just want to check location.hash:



/^#d+$/.test(location.hash);


/^#d+$/ will match a hash that contains 1 or more digits and nothing else.


[#61933] Monday, May 30, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
diane

Total Points: 264
Total Questions: 104
Total Answers: 95

Location: Liechtenstein
Member since Wed, Dec 8, 2021
3 Years ago
;