0

i would like to write a function like this

let string = "s4D$XLssksXKKLX";

//this alphabet is in array, but i can convert to other
let alphabet = [s4L1Xl7CbKrTD8Uo_NPdVWiI9zpBfZMm20hJ$qA5kuQta36yFevxnYHcEGRjgwSO]

if (string is contained in alphabet)
   return true
else
   return false

can someone help me?

thank you guys!

Marco Tribuzio
  • 103
  • 1
  • 1
  • 6
  • 1
    please clarify what you mean by "the string is contained"... exactly matching part of an item inside the array? Or just all the same characters are in both? – Mottie Dec 19 '16 at 21:17
  • i mean this, for example the string s4D$XLssksXKKLX match with my custom alphabet, instead the string s4D@XLssksXKKLX with the @ character don't match with the alphabet. I'm sorry for my bad English, I answered you? – Marco Tribuzio Dec 19 '16 at 21:21
  • Your alphabet contains duplicate letters. – Redu Dec 19 '16 at 22:03

4 Answers4

1

The indexOf string method does exactly this:

let alphabet = "s4L1Xl7CbKrTD8Uo_NPdVWiI9zpBfZMm20hJ$qA5kuQta36yFevxnYHcEGRjgwSO";

function testString(input){
  if (alphabet.indexOf(input) > -1){
     return true;
  } else {
     return false ;
  } 
}

console.log(testString("s4D$XLssksXKKLX"));
console.log(testString("CbKrTD8U"));
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
0

Javascript has a string utility method called indexOf. You can refer to this [stackoverflow post] :How to check whether a string contains a substring in JavaScript?

Community
  • 1
  • 1
tiwsetd
  • 11
  • 2
0

With ES6 you can use String.prototype.includes() method that determines whether one string may be found within another string, returning true or false as appropriate:

let alphabet = 's4L1Xl7CbKrTD8Uo_NPdVWiI9zpBfZMm20hJ$qA5kuQta36yFevxnYHcEGRjgwSO';

console.log(alphabet.includes('s4D$XLssksXKKLX'));
console.log(alphabet.includes('0hJ$qA5k'));
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
  • thank you yosvel, but in my idea, both string 0hJ$qA5k and sssssssss, belong to the alphabet. the string that not belong to the alphabet is for example 0hJ@qA5k. I managed to explain to me? Sorry for my bad english! – Marco Tribuzio Dec 19 '16 at 22:03
  • thank you Yosvel, but why if have this string s4D$XLssksXKKLX the function return false, instead of true? – Marco Tribuzio Dec 19 '16 at 22:19
  • I have added an example `alphabet.includes('s4D$XLssksXKKLX')` and it logs is `false` because the string `s4D$XLssksXKKLX` is not included in the `alphabet` variable – Yosvel Quintero Dec 19 '16 at 22:40
0

I solved my problem with this regEx

(text).match(/^([s4L1Xl7CbKrTD8Uo_NPdVWiI9zpBfZMm20hJ$qA5kuQta36yFevxnYHcEGRjgwSO])+$/g))

in this way, the string of any size must belong to this custom alphabet :)

Marco Tribuzio
  • 103
  • 1
  • 1
  • 6