MandyM
MandyM

Reputation: 87

Javascript iterate over string looking for multiple character sets

Ok, so I know how to do a standard loop to iterate over a string to find a character or a word that matches a single character or word, but in this instance, I have multiple character sets that I am looking for. Some are letters, some have characters (including protected ones). I can't split it into an array of words on space or anything like that because the character sets might not have a space, so wouldn't split. I suspect I'm going to have to do a regex, but I'm not sure how to set it up. This is basically the pseudo code of what I'm trying to do and I'd appreciate any tips on how to move forward. I apologize if this is an easy thing and I'm missing it, I'm still working on my javascript.

Pseudo code:

var string = "This *^! is abdf random&!# text to x*?ysearch for character sets";
var tempSet = [];

// start a typical for loop
for(string.length bla bla...){
  // look for one of those four character sets and if it hits one 
  if(foundSet == "abdf" | "x*?y" | "*^!" | "&!#")
    // push that character set to the tempSet array
    tempSet.push(foundSet);
    // continue searching for the next set until the string is done 

console.log(tempSet);  
//expected result = ["*^!", "abdf", "&!#", "x*?y"]

and all the sets are in the array in the order in which they appeared in the string

there is obviously more, but that part I can handle. It's this line

if(??? == "abdf" | "x*?y" | "*^!" | "&!#")

that I don't know really how to tackle. I suspect it should be some kind of regex but can you have a regex like that with a | when doing an if statement? I've done them with a | when doing a map/replace but I've never used a regex in a loop. I also don't know how to get it to search multiple characters at a time. Some of the character sets are 3, some are 4 characters long.

I would appreciate any help or if you have a suggestion on how to approach this in an easier way, that would be great.

Thanks!

Upvotes: 1

Views: 3244

Answers (4)

Jared Farrish
Jared Farrish

Reputation: 49188

A bit more of a manual method than Barmar's excellent RegEx, but it was fun to put together and shows the pieces maybe a bit more clearly:

var text = "This *^! is abdf random&!# text to x*?ysearch for character sets",
    detect = ["abdf", "x*?y", "*^!", "&!#"],
    haystack = '',
    found = [];

text.split('').forEach(function(letter){
    haystack += letter;
    detect.forEach(function(needle){
        if (haystack.indexOf(needle) !== -1
             && found.indexOf(needle) === -1) {
            found.push(needle);
        }
    });
});

console.log(found);

Upvotes: 2

Barmar
Barmar

Reputation: 780808

You can use a regular expression. Just list all your strings as alternatives separated by |. Characters that have special meaning in regular expressions (e.g. *, ?, ^, $) will need to be escaped with \ (you can safely escape any non-alphanumeric characters -- some will be redundant).

var string = "This *^! is abdf random&!# text to x*?ysearch for character sets";
var tempSet = string.match(/abdf|x\*\?y|\*\^!|&!#/g);

console.log(tempSet);

If you need a loop you can call RegExp.prototype.exec() in a loop.

var string = "This *^! is abdf random&!# text to x*?ysearch for character sets";
var regex = /abdf|x\*\?y|\*\^!|&!#/g;
var tempSet = [];
while (match = regex.exec(string)) {
  tempSet.push(match[0]);
}
console.log(tempSet);

Upvotes: 2

Russell
Russell

Reputation: 496

I would just add this as a comment to Kevin's answer if I was able to, but if you need IE support you can also check searchString.indexOf(searchToken) !== -1.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf

Upvotes: 1

Kevin
Kevin

Reputation: 199

I think what you're looking for is the includes() function.

var sample = "This *^! is abdf random&!# text to x*?ysearch for character 
sets";
var toSearch = ["*^!", "abdf", "&!#", "x*?y"];
var tempSet = [];

for (var i = 0; i < toSearch.length; i++) {
    if (sample.includes(toSearch[i]){
        tempSet.push(toSearch[i]);
    }
}

console.log(tempSet);  
//expected result = ["*^!", "abdf", "&!#", "x*?y"]

This way you can iterate through an entire array of whatever strings you're searching for and push all matching elements to tempSet.

Note: This is case sensitive, so make sure you consider your check accordingly.

Upvotes: 1

Related Questions