I am trying to evaluate if a string is equal to one of these symbols |\~+
But this doesn't work:
var a = 'abcted',
b = '|\~+';
if( a !== b ){
}
So if a is equal to one of the symbols in b, is evaluates to false.
I am trying to evaluate if a string is equal to one of these symbols |\~+
But this doesn't work:
var a = 'abcted',
b = '|\~+';
if( a !== b ){
}
So if a is equal to one of the symbols in b, is evaluates to false.
You can simply check if one string contains another using the method indexOf:
var b = '|\~+';
if (b.indexOf(a) == -1) {
// b doesn't contain a
}
A new suggestion is made for the function includes, which would work like this, but not (yet) in many browsers.
var b = '|\~+';
if (!b.includes(a)) {
// b doesn't contain a
}
You can use Regular Expression to solve this, like this
if (/[|\\~\+]/.test(a) === false) {
// `a` doesn't contain any of the symbols.
}
[|\\~\+] checks if any of the characters in the character group is in a. If none of them are there then test will return false.
console.log(/[|\\~\+]/.test("abcted"));
// false
console.log(/[|\\~\+]/.test("abc|ted"));
// true
console.log(/[|\\~\+]/.test("abc\\ted"));
// true
console.log(/[|\\~\+]/.test("abc~ted"));
// true
console.log(/[|\\~\+]/.test("abc+ted"));
// true
I prefer to use RegEx where possible, but for those who are not wanting to:
var a = 'abcted',
b = '|\~+';
for (var i = 0, len = b.length; i < len; i++) {
if (a.indexOf(b[i]))
{
alert(b[i]);
}
}