Matching a String with Array of String in Java Script

javascript array contains string
indexof javascript
javascript regex
javascript regex multiple matches
javascript match
search string in array javascript
javascript string contains
javascript regex match group

    str1 = booking_kode.substring(0, 3);
    B = ["800", "807", "826", "847", "866"];
    C = ["827", "846"];
    E = ["867", "879"];
    F = ["880", "899"];

    if (str1 = array B){
    	print ('Prefix , first 3 digit = ' + str1 + '\n')
    	comm_code = 'B000'
    	print ('Comm_Code = ' + comm_code + '\n')
    }
    else if (str1 = array C) {
	print ('Prefix , first 3 digit = ' + str1 + '\n')
	comm_code = 'C000'
	print ('Comm_Code = ' + comm_code + '\n')
}
    else if (str1 = array E) {
	print ('Prefix , first 3 digit = ' + str1 + '\n')
	comm_code = 'E000'
	print ('Comm_Code = ' + comm_code + '\n')
}
    else if (str1 = array F) {
	print ('Prefix , first 3 digit = ' + str1 + '\n')
	comm_code = 'F000'
	print ('Comm_Code = ' + comm_code + '\n')
}
    else {
	print ('Prefix , Nilai 3 digit pertama = ' + str1 + '\n')
	comm_code = 'D000'
	print ('Comm_Code = ' + comm_code + '\n')
}

Just use a simple String.prototype.indexOf:

str1 = booking_kode.substring(0, 3);
B = ["800", "807", "826", "847", "866"];
C = ["827", "846"];
E = ["867", "879"];
F = ["880", "899"];

if (B.indexOf(str1) > -1)
{
    print ('Prefix , first 3 digit = ' + str1 + '\n');
    comm_code = 'B000';
    print ('Comm_Code = ' + comm_code + '\n');
}
else if (C.indexOf(str1) > -1)
{
    print ('Prefix , first 3 digit = ' + str1 + '\n');
    comm_code = 'C000';
    print ('Comm_Code = ' + comm_code + '\n');
}
else if (E.indexOf(str1) > -1)
{
    print ('Prefix , first 3 digit = ' + str1 + '\n');
    comm_code = 'E000';
    print ('Comm_Code = ' + comm_code + '\n');
}
else if (F.indexOf(str1) > -1)
{
    print ('Prefix , first 3 digit = ' + str1 + '\n');
    comm_code = 'F000';
    print ('Comm_Code = ' + comm_code + '\n');
}
else
{
    print ('Prefix , Nilai 3 digit pertama = ' + str1 + '\n');
    comm_code = 'D000';
    print ('Comm_Code = ' + comm_code + '\n');
}

JavaScript Includes To Search In Strings & Arrays, The JavaScript includes method finds the target in an Array or to Use JavaScript includes to Find Matches in Strings and Arrays. Example to convert JavaScript String to an array using delimiter and limit. If we want to retrieve only 2 words then we specify limit as 2 and use blank space as a separator. This is another way to convert string to array in Javascript. By specifying the limit parameter, we can control the number of words we want to get.

You can achieve this with simple if else conditions using Array.indexOf() Methods. However make sure the str1 and the values in the array are of same variable type(string or a number).

   if (B.indexOf(str1) > -1 ) {   //if value exists in B 
        //do soemthing;
    } 
    else if(C.indexof(str1) >-1 ) { //if value exists in C
      //do soemthing
    }

String.prototype.match(), The matchAll() method returns an iterator of all results matching a string iteration // Call matchAll again to create a new iterator Array.from(str. The match () method searches a string for a match against a regular expression, and returns the matches, as an Array object. Read more about regular expressions in our RegExp Tutorial and our RegExp Object Reference.

One possible solution to solve this is use Array.some() (I think it is included on ES5 based on this link). First you can create a method that will check if an element is on an array:

function arrayIncludes(arr, ele)
{
     return arr.some(function(x) {return (x === ele);});
}

console.log(arrayIncludes([1,2], 2));
console.log(arrayIncludes([1,2], 5));
.as-console {background-color:black !important; color:lime;}

String.prototype.matchAll(), match() is an inbuilt function in JavaScript which is used to search a string for a match against a any regular expression and if the match will found then this will return the match as an array. Parameters: Here the parameter is “regExp” i.e, regular expression which will compare with the given string. Java Array of Strings. Java String Array is a Java Array that contains strings as its elements. Elements of no other datatype are allowed in this array. In this tutorial, we will learn how to declare a Java String Array, how to initialize a Java String Array, how to access elements, etc.

JavaScript, JavaScript String match() Method The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object. Sorting Arrays of Strings In JavaScript arrays have a sort( ) method that sorts the array items in Tagged with arrays, javascript, webdev, programming.

JavaScript String match() Method, str.match(regexp) finds matches for regexp in the string str . it returns the first match as an array with capturing groups and� The method str.match(regexp) finds matches for regexp in the string str. It has 3 modes: If the regexp doesn’t have flag g, then it returns the first match as an array with capturing groups and properties index (position of the match), input (input string, equals str):

Methods of RegExp and String, Here's the situation: 1) You have a string in JavaScript, like var fruit = ''. then see whether the value matches one of the values in the array: Convert String to Array with JavaScript's split Method JavaScript's string split method returns an array of substrings obtained by splitting a string on a separator you specify. The separator can be a string or regular expression. Invoke the split method on the string you want to split into array elements.

Comments
  • I have added the full code to your answer, feel free to edit it.
  • includes is ES6 - OP can only use ES5. indexOf is the correct answer.
  • No problem @Shidersz.
  • @JackBashford I have reworked the code to use Array.some(). I have read it is available on ES5 from different sources.