0

I want to this : When the entered value matches the value of the my array , write the matches value.

so this is my code :

//my array as the following :

var checkNames = (document.getElementById("KPIorCounterList").value.split("\n").map(element => element.trim())).filter(v=>v != "");

my another array : myarray= ["RAB Video call drop %",RAB PS R99 drop % ","RAB PS HSDPA drop %"]

For example ; when write RAB* to textare, I should to see, starting with "RAB"

I guess, My code should be as follows:

  for (var i = 0; i < checkNames.length; i++) {
                    for (var j = 0; j < myarray.length; j++) {
           // var str = myarray[j].split(" "); // I am not sure for his.

            I want to this for here : (pseudo code)
            for example checkName[i] == RAB*
            if (checkName[i].match("match condition") == myarray[j])
            alert(myarray[j]);
            //I should show output myarray[j] == RAB Video call drop %",RAB PS R99 drop % ","RAB PS HSDPA drop %"
       }
    }

How can I do ? Please..

eagle
  • 451
  • 1
  • 7
  • 27

2 Answers2

1

@eagle, I believe this is the input to checkNames. If so, here is my solution -

var checkNames = ['RAB*'];
var myarray = ["RAB Video call drop %","RAB PS R99 drop % ","RAB PS HSDPA drop %"];

for (var i = 0; i < checkNames.length; i++) {
    for (var j = 0; j < myarray.length; j++) {
        var formatRegExpr = checkNames[i].replace('*','.*');
        var re = new RegExp(formatRegExpr, 'g');
        alert(myarray[j].match(re));
    }
}

Accept this answer if it solves your requirement.

dinesh
  • 342
  • 2
  • 13
  • I'm trying to learn so `var re = new RegExp(formatRegExpr, 'g');` What 'g' is? @dinesh – eagle Jul 01 '16 at 07:48
  • g - global search flag. Good place to learn - [regexr-dot-com](http://regexr.com/) – dinesh Jul 01 '16 at 07:52
  • checkName came from 'textarea.value '. I want to this : textarea.value is array and if array eleman is : ''RAB*'' the above code should work. How can I control it ? please @dinesh – eagle Jul 01 '16 at 08:02
1

function check(){
var checkNames = (document.getElementById("KPIorCounterList").value.split("\n").map(element => element.trim())).filter(v=>v != "");
var myarray = ["RAB Video call drop %", "RAB PS R99 drop % ", "RAB PS HSDPA drop %"]
for (var i = 0; i < checkNames.length; i++) {
  console.log("results for", checkNames[i])
  for (var j = 0; j < myarray.length; j++) {

      var matchString = myarray[j].match(new RegExp(checkNames[i].replace('*','.*')));
    if (matchString && myarray.indexOf(matchString[0])!==-1) {
      console.log(myarray[j]);
    }
    //I should show output myarray[j] == RAB Video call drop %",RAB PS R99 drop % ","RAB PS HSDPA drop %"
  }
}
  }
<textarea id="KPIorCounterList"></textarea>
<button onclick="check()">Check</button>
Vladu Ionut
  • 8,075
  • 1
  • 19
  • 30
  • checkName came from 'textarea.value '. I want to this : textarea.value is array and if array eleman is : ''RAB*'' the above code should work. How can I control it ? please @VladuIonut – eagle Jul 01 '16 at 08:04