Jump to content

Regexp(): How Can I Use Variables In This Syntax (/var/gi)


tinfanide

Recommended Posts

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><script>str = "This is an apple.";function searching(query){document.getElementById("findings").innerHTML = str.replace(/query/gi,"<span style='color: red'>"+query+"</span>");}</script></head><body><div id="text"><script>document.write(str)</script></div><form><input type="text" id="querys" value="" onkeyup="searching(this.value)" /></form><div id="findings">Results</div></body></html>

For this

/query/gi

I know I can use

new RegExp(query,"gi")

But just wonder how I can use variables in the / / syntax.

Link to comment
Share on other sites

But now the problem is I have to use quantifiers or MetaCharacters and I see all people using the /regexp/ and no one uses new RegExp()but if I needa pass variables into RegExp, I can only use RegExp(). How can I use both MetaCharacters and passing parameters in JS regular expression?

Link to comment
Share on other sites

Even I do something like this,it seems impossible in JS:

new RegExp("\s"+query+"\s","gi")

It seems that if I want to search for the whole word of my query entered in the form (that needs to pass the parameter)totally impossible in JS...because I cannot use \s \s which it is okay in /regexp/ :umnik2:

Link to comment
Share on other sites

Try

new RegExp("\\s"+query+"\\s","gi")

(I believe the JavaScript interpreter may be interpreting "\s" in some fashion before giving it to the RegExp object; escaping the slash will prevent that)

Link to comment
Share on other sites

Good, man. I've always learnt a lot from you guys. Thanks. Had read something like the use of "\\" before in some tutorials but just missed this important usage.

Link to comment
Share on other sites

str = "This is an apple.This is an orange. That is an elephant. Thisa loves it.";alert(str.match(new RegExp("\\s"+"an"+"\\s","gi")).length);alert(str.match(new RegExp("\\s"+"This"+"\\s","gi")).length);

Consider this example. I found searching for this quite interesting. It seems to be only "this" that cannot be searched.Do we need to use escape in this case?

Link to comment
Share on other sites

str = "This is an apple. This is an orange. That is an elephant. Thisa loves it.";/*alert(str.match(new RegExp("\\s"+"an"+"\\s","gi")).length);alert(str.match(new RegExp("\\s"+"This"+"\\s","gi")).length);    document.getElementById("div").innerHTML = str.match(new RegExp("\\s"+document.getElementById("input").value+"\\s","gi")).length;*/function check(){if(str.charAt(str.indexOf(document.getElementById("input").value)+document.getElementById("input").value.length)=="."){    var patt = new RegExp(document.getElementById("input").value+".","gi");    document.getElementById("div").innerHTML = "There are "+str.match(patt).length+" of this search item.";  }if (str.indexOf(document.getElementById("input").value)==1) {    var patt = new RegExp(document.getElementById("input").value+"\\s","gi");    var patt1 = new RegExp("\\s"+document.getElementById("input").value+"\\s","gi")    document.getElementById("div").innerHTML = "There are "+str.match(patt).length + str.match(patt1).length+" of this search item.";   }if (str.match(patt)==null){    document.getElementById("div").innerHTML = "There is none of this search item.";    }   if (document.getElementById("input").value == ""){	 document.getElementById("div").innerHTML = "Search for something you like"+"<br />"+"Get the number of the search item.";	 }	if (str.charAt(str.indexOf(document.getElementById("input").value)-1)==" " && str.charAt(str.indexOf(document.getElementById("input").value)+document.getElementById("input").value.length)==" ") {    var patt = new RegExp("\\s"+document.getElementById("input").value+"\\s","gi");    document.getElementById("div").innerHTML = "There are "+str.match(patt).length+" of this search item.";          /*    for( var count = 0; patt.exec(str); ++count );    document.getElementById("div").innerHTML = count;    */       }}window.onload = function(){document.getElementById("str").innerHTML = str;document.getElementById("div").innerHTML = "Search for something you like"+"<br />"+"Get the number of the search item.";};

Yes, that's why I wrote another one and there was still some strange behaviour in FF and IE.If ya have time, ya may check it.I found that the number of "this" is 1, not 3.And "is" cannot be found (none). It should be 3. But "an" can be counted (as 3).

Link to comment
Share on other sites

There's only one single "This" in your string that's preceded by a space and followed by a space. As for the "is" search, if you're getting "is" from an input, be sure that nothing else got sent from the input, like whitespace.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...