I've written the following function in Typescript:
public searchPosts(keyword ? : string): Post[] {
return this._posts.filter(function(post) {
if (post.title.search(new RegExp('money', 'gi')) >= 0) {
return post;
}
});
}
It's working just fine but, I need to make it a little dynamic so that instead of hardcoded value i.e. money, I can put my keyword variable in RegExp (new RegExp(keyword, 'gi')). But doing so does not return anything even for 'money' as a keyword.
Any ideas on what I'm doing wrong here?
'gi'withkeyword? --- Hmm.(keyword = '') => this._posts.filter(post => post.title.search(new RegExp(keyword, 'gi')) >= 0)filtershould returntrueorfalseReturns the elements of an array that meet the condition specified in a callback function.