0

Next, I have this div below where certain tags contains the type [INFO] [/ INFO]:

<div id="tags">
[INFO id=“rol”]
Rol -> is a greatest project to add in the same people
[/INFO]


[INFO id=“add”]
Add -> is moment who someone pick the ball and shoot in the cars
[/INFO]


[INFO id=“tree”]
tree -> is tree of a views in the other dimensions
[/INFO]
</div>

I created a search engine made ​​in jQuery, where you type something in the text field and click search so that it saves the variable 'SearchText', as you can see below:

$(“#buttonGo").click(function() {
var searchText = $(‘#searchField’).val();
});

After that all I have to do is:

  1. Take the value stored in the variable SearchText, and look for text in div

  2. Then find the text so give a warning showing in which it is located INFO tag, eg:

SearchText var = "views in the other";

  He must recognize the tag [INFO id = "tree"] [/ INFO] because the text is located within this tag .... and return the alert should come as follows:

(Tag: Info, Id: tree, desc:"tree -> is tree of a views in the other dimensions")

I do not know where to start, can anyone give me a hand?

user3372120
  • 134
  • 1
  • 10
  • http://stackoverflow.com/questions/926580/find-text-string-using-jquery – Zach Saucier Mar 24 '14 at 15:04
  • For starters use the link in Zach Saucier comment to find the div you want. As with most programming problems, solving it is a matter of style. One way is to create an `INFOARRAY` object which contains the various `INFO` items. Then, loop through these and check if it contains text that matches the search string. – Daniel Kotin Mar 24 '14 at 15:17
  • And PHP, java, C++ Can do this with more facility? – user3372120 Mar 24 '14 at 15:46
  • I've added a quick javascript solution below so you can reference that – Daniel Kotin Mar 24 '14 at 15:56

1 Answers1

1

To follow up on my earlier comment, you can create an INFO object like this:

var treeInfo = new Object();
treeInfo.id = "tree";
treeInfo.description = "tree -> is tree of a views in the other dimensions"

Next, create your array to hold the info objects if you so desire:

var infoArray = [];
infoArray.push(treeInfo); 

Then when you're checking the provided text, use a simple loop. eg:

for (var i = 0; i < infoArray.length; i++) {
     if (infoArray[i].description.indexOf(searchTextVar) != -1){
       //found correct text, do something.
     }
}

It will probably be better to wrap the for loop in a function and return the Info object when you find a match.

You can use this path if you don't want to store the INFO objects in a div.

Edit to address comment about array size: If you want to use a huge block of text you can use the method below though it isn't necessarily the cleanest/fastest. It is just one way of solving the problem.

Use a regex like this: \[INFO id=.+?] once starting from the index of searchTextVar. And then, find the closing tag from that same index with something like this \[/INFO]. Now, a substring of your searchTextVar using the indices from the regex matches will contain your tag.

After that, your resulting string should be the right tag and you can parse the text and display it as appropriate.

If you're dealing with that many tags, it might not be the best idea to store them in a browser... consider using a database or some other storage mechanism

Daniel Kotin
  • 326
  • 2
  • 8
  • Ok lets think, A array with 120000 arrays? it come slow? – user3372120 Mar 24 '14 at 15:58
  • That wasn't a requirement in your previous question... It would help a lot if you update your previous question with such details. If you want to store it as text in a `div`, then when you get the object, I would suggest you get the index of the text you want, and use a `regex` to grab the whole `INFO` tag surrounding it – Daniel Kotin Mar 24 '14 at 16:05