0

Im looking for a solution to search the existence of given characters in a string. That means if any of the given characters present in a string, it should return true.

Now am doing it with arrays and loops. But honestly I feel its not a good way. So is there is any easiest way without array or loop?

var special = ['$', '%', '@'];
var mystring = ' using it to replace VLOOKUP entirely.$ But there are still a few lookups that you are not sure how to perform. Most importantly, you would like to be able to look up a value based on multiple criteria within separate columns.';
var exists = false;
$.each(special, function(index, item) {
  if (mystring.indexOf(item) >= 0) {
    exists = true;
  }
});
console.info(exists);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Sandeep Thomas
  • 4,303
  • 14
  • 61
  • 132

3 Answers3

1

try with regex

var patt = /[$%@]/;
console.log(patt.test("using it to replace VLOOKUP entirely.$ But there are still a few lookups that you are not sure how to perform. Most importantly, you would like to be able to look up a value based on multiple criteria within separate columns."));
Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39
1

Be aware that [x] in regEx is for single characters only.

If you say wanted to search for say replace, it's going to look for anything with 'r,e,p,l,a,c' in the string.

Another thing to be aware of with regEx is escaping. Using a simple escape regEx found here -> Is there a RegExp.escape function in Javascript? I've made a more generic find in string.

Of course you asked given characters in a string, so this is more of an addenum answer for anyone finding this post on SO. As looking at your original question of an array of strings, it might be easy for people to think that's what you could just pass to the regEx. IOW: your questions wasn't how can I find out if $, %, @ exist in a string.

var mystring = ' using it to replace VLOOKUP entirely.$ But there are still a few lookups that you are not sure how to perform. Most importantly, you would like to be able to look up a value based on multiple criteria within separate columns.';

function makeStrSearchRegEx(findlist) {
  return new RegExp('('+findlist.map(
  s=>s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')).join('|')+')');
}

var re = makeStrSearchRegEx(['$', '%', '@', 'VLOOKUP']);


console.log(re.test(mystring));  //true
console.log(re.test('...VLOOKUP..')); //true
console.log(re.test('...LOOKUP..'));  //false
Keith
  • 22,005
  • 2
  • 27
  • 44
  • Good attempt at extending it, but I'd use [this one](https://stackoverflow.com/a/3561711/989121) for escaping. – georg Sep 21 '17 at 11:13
0

The best way is to use regular expressions. You can read more about it here.

In your case you should do something like this:

const specialCharacters = /[$%@]/;
const myString = ' using it to replace VLOOKUP entirely.$ But there are still a few lookups that you are not sure how to perform. Most importantly, you would like to be able to look up a value based on multiple criteria within separate columns.';
if(specialCharacters.test(myString)) {
   console.info("Exists...");
}

Please, note, that it is good approach to store regular expressions in a variable to prevent creating of regular expression (which is not the fastest operation) each time you use it.

Dmytro Medvid
  • 4,988
  • 3
  • 25
  • 29