regex - javascript split and match not working (Parse scalable search) -
i trying implement scaleable search per parse docs.
below code using pretty lifted post.
var _ = require("underscore"); parse.cloud.beforesave("exercises", function (request, response) { var exercise = request.object; console.log("log1" + exercise.get("exercisename")); var tolowercase = function (w) { return w.tolowercase(); }; var words = exercise.get("exercisename").split(/b/); words = _.map(words, tolowercase); console.log("log 2" + words); var stopwords = ["the", "in", "and", "with", "on"] words = _.filter(words, function (w) { return w.match(/^w+$/) && !_.contains(stopwords, w); }); console.log("log 3" + words); var hashtags = exercise.get("exercisename").match(/#.+?b/g); hashtags = _.map(hashtags, tolowercase); exercise.set("words", words); exercise.set("hashtags", hashtags); response.success(); });
here output @ each log.
log 1: squats dumbells
log 2: squats dumbells
log 3:
it seems following line:
words = _.filter(words, function(w) { return w.match(/^w+$/) && ! _.contains(stopwords, w); });
nothing returned. in log 2 expected given ["squats", "with","dumbells"]
my hashtags
variable empty, there issue match
in code?
here workding jsfiddle log statements
you don't need regex inside filter function. if want check w
word, missing \
right before w
in regex. works expected:
var stopwords = ["the", "in", "and", "with", "on"]; words = _.filter(words, function(w) { return w.match(/^\w+$/) && ! _.contains(stopwords, w); // ^ 1 }); console.log(words); // [ "squats", "dumbells" ]
the reason hashtags empty because have no hashtags in exercise.get("exercisename")
. also, have words in words
array. hashtags checking if first character #
:
var hashtags = _.filter(words, function(word) { return word.indexof("#") === 0; });
Comments
Post a Comment