javascript - Why isn't my regex matching the entire document? -


i'm trying build small javascript application loads third party site, finds given word , highlights closest context of document using jquery extension highlight (with small customization allowing regular expressions).

first, i'm trying application highlight surrounding setting context 500 characters, reason cuts off in weird places. this article, i'm trying match term obama, , can see screenshot, cuts off in places shouldn't be.

does have clue of what's going on?

$(document).ready(function() {     $.get(geturlvars()["url"],     function(data) {         var fdata = $(data);         var associationscope= 500;           $.each(geturlvars()["topics"].split(","), function(index, value) {             if (geturlvars()["associationscope"] == "context") {                 var associationscoperegex = "((?!</span>)[\\s\\s]{0," + associationscope + "})"                      + value + "((?!<span class=\"associationscope\">)[\\s\\s]{0," + associationscope + "})";                  fdata.highlight(associationscoperegex, {classname: "associationscope"});             }              fdata.highlight(value, {classname: "topichighlight"});         });          $("#externalpage").html(fdata);      }); }); 

screenshot of highlighting result

you need escape regex metacharacters (well backslash in case) when build via strings:

   var associationscoperegex = "((?!</span>)(.|\\n|\\r|\\t){0," + associationscope + "})"         + value + "((?!<span class=\"associationscope\">)(.|\\n|\\r|\\t){0," + associationscope + "})"; 

when build regex string, have take account fact javascript parser doesn't know string going regex; parses string. syntax string constants uses backslashes special characters, interpreted thusly part of string.

(you don't have double-up on backslashes double-quote characters, because it's ok leave them simple double-quotes regex.)


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -