theUnlearned theUnlearned - 1 year ago 82
jQuery Question

JS specific search function

Hi i've got some root in javascript basics, but i'm trying to get into jquery since i assume it has a better search system or function. Either way, what i'd like help in is to create a type of search that would scan through the data contained in a variable and use it to automatically create objects using a constructor. But lets start small.
The particular thing i'm trying to figure out, is creating a search function that will recognize what word or words i'd like extracted and store to another variable to use later.

Here is a little visual of what i need done.

Lets say i have the variables below.

textData = "&@^%! $%)#AAx1<# >^$(!($< BBx2<((@!^@(#^%))CCx24 33 80<#%#* ";
var name1;
var name2;
var list1[];

I need to scan "textData" until i get to the letter Combos, or markers such as "AAx", then take the next item, the number(s) "1" and store it to variable "name1" and so forth until the variables are filled as follows:

name1 = 1;
name2 = 2;
list1 = [24, 33, 80];

A little more explanation. Notice that when the markers AAx, BBx and CCx were recognized, the numbers that followed were what was recorded/stored. In particular, the array "list1", it continued to store the numbers until the next element was not a number, rather was a smaller-than sign "<", similar to name1 and name2's situation.

Most of the examples of js search tutorials i've seen online deal with a whole word between two spaces or individual alphanumeric/symbol pickers. What I'm sure i'll need to do is use a loop with an if statement that would find the first match of "AAx" and then i'm lost for how to write code that will take info that follows that marker, then stop at "<". Then continue onto the next markers, BBx and CCx.
I appreciate any help given.

Answer Source

String functions used

  1. Replace ( /xxx/g is used for a global replace)
  2. Split

Array functions used

  1. forEach
  2. shift
  3. Map

Number Functions

  1. Number Constructor (map to Number converts each item to numeric value)

var string = "&@^%! $%)#AAx16<# >^$(!($< BBx2<((@!^@(#^%))CCx24 33 80<#%#*&@^%! $%)#AAx16<# >^$(!($< BBx2<((@!^@(#^%))CCx24 34 81<#%#*&@^%! $%)#AAx16<# >^$(!($< BBx2<((@!^@(#^%))CCx24 35 82<#%#*"; 
var result = [];
//Result: TOParray = [ [16, 2, [24,33,80]] [16, 2, [24,33,80]] [16, 2, [24,33,80]] ];
var arr = string
    .replace(/AA/g, '<CUTHERE>AA')//set markers to cut
    .split('<CUTHERE>');//split at each marker
arr.shift();//remove first element which doesnt have any value
    .forEach(function(item){//iterate through each element
        var a = +/AAx(\d+)*/.exec(item)[1];//get number followed by AAx
        var b = +/BBx(\d+)*/.exec(item)[1];//get number followed by BBx
        var c = /CCx([\d\s]+)*/.exec(item)[1].split(" ").map(Number);//get number and spaces followed by BBx and split it at spaces and convert them to number
        result.push([a,b,c]);//push these to result array 


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download