Currently, I am working on an exercise within a Javascript learning platform.
The task is as follows:
Input: A string consisting of words, with some words possibly containing hashtags (prefixed with #).
Output: An array of strings that have been prefixed with a hashtag but do not contain the actual hashtag character.
It's important to note that single pound signs alone are not considered valid inputs (e.g., "#" would result in an empty array).
- If a word has multiple leading hashtags, only the last one should be considered (e.g., "##example" would return ["example"])
- Hashtags must not appear within the middle of a word (e.g., "in#line hashtag" should return an empty array)
- Furthermore, hashtags must come before alphabetical characters (e.g., "#123abc" or "#?" are invalid inputs)
This was my attempt at solving the problem:
function getHashtags(post) {
return /#(\w+)/.exec(post)
}
However, the output I'm receiving looks like this:
String Input: Hello #world
Outpu t: [ '#world', 'world', index: 6, input: 'Hello #world' ]
String Input: #lol #sorryNotSorry #heya #coolbeans
Output: [ '#lol','lol', index: 0, input: '#lol #sorryNotSorry #heya #coolbeans']
String Input: # # # #
Output: null
String Input: this is an in#line hash
Output: [ '#line', 'line', index: 13, input: 'this is an in#line hash' ]
String Input: too ##many tags
Output: [ '#many', 'many', index: 5, input: 'too ##many tags' ]
String Input: invalid chars #$? #;wha
Output: null
String Input: "" //empty string
null
String Input: #blue#red#yellow#green
Output:[ '#blue', 'blue', index: 0, input: '#blue#red#yellow#green' ]
I believe I may need to implement lookback functionality, although it's not directly supported in Javascript and I haven't found a suitable workaround yet. Can anyone offer any guidance?