search for keyword in extracted text javascript -



search for keyword in extracted text javascript -

so managed extract text, , saved variable later use, how can test keywords within said text?

here's example, checktitle text extracted, , want search keywords, in example, words delimited commas within comparetitle. want search strings '5' , 'monkeys'.

var checktitle = "5 monkeys jumping on bed"; var comparetitle = "5 , monkeys"; if (checktitle === comparetitle) { // ... }

you can utilize regular expressions search strings, , test() function homecoming true/false if string contains words.

/(?:^|\s)(?:(?:monkeys)|(?:5))\s+/gi.test("5 monkeys jumping on bed");

will homecoming true, because string contains either (in case, both) words 5 , monkeys.

see illustration here: http://regexr.com/39ti7 utilize site's tools analyse each aspect of regular expression.

if need alter words testing each time, can utilize code:

function checkformatches(){ var words = checktitle.split(" , "); var patt; (var = 0; < words.length; i++){ patt = patt + "(" + words[i] + ")|"; } patt[patt.length-1] = ""; patt = new regexp(patt, "i"); homecoming patt.test(checktitle); }

will homecoming true, , allow words want checked against. must separated this:

comparetext = "word1 , word2 , word3 , on , ..."; // ^^^notice, space-comma-space.

and can utilize in if statement this:

var checktitle = "5 monkeys jumping on bed"; var comparetitle = "5 , monkeys"; if (checkformatches()){ //a match found! }

javascript

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -