Generates all right substrings of a given string.
String.prototype.length
to terminate early if the string is empty.for...in
loop and String.prototype.slice()
to yield
each substring of the given string, starting at the end.const rightSubstrGenerator = function* (str) {
if (!str.length) return;
for (let i in str) yield str.slice(-i - 1);
};
[...rightSubstrGenerator('hello')];
// [ 'o', 'lo', 'llo', 'ello', 'hello' ]
Snippet collection
JavaScript generator functions are a more advanced yet very powerful JavaScript ES6 feature, which you can start using in your code right now.
JavaScript, String
Finds all the indexes of a substring in a given string.
JavaScript, String
Generates all left substrings of a given string.
JavaScript, String
Counts the occurrences of a substring in a given string.