Reputation: 750
I'm studying regular expressions in Javascript
I've seen many ways to do exclusive matching through the OR operator with []
and |
within groups ()
.
I can't understand how to achieve the AND behavior with regular expressions. I've done some research but I didn't find what I need.
Here an example. I have the following string: kata
. I want to compare it with another string: steak
.
The goal is to return true
if all the letters in steak
is contained in kata
. If I use this regular expression [steak]
it returns true but actually it should return false because in kata there is no "s".
Example 2. String1 = scriptsjava
, string2 = javascript
, result = true
(because string2 is contained in string1)
Example 3. String1 = jscripts
, string2 = javascript
, result = false
(because string2 is not fully contained in string1)
Example 4. String1 = rkqodlw
, string2 = world
, result = true
(because the string world is in the first string)
I thought that using regular expressions is the best way and I considered string2 as a pattern. My solution to this problem is the following
var validate=true;
var counter = 0;
str2.split("").map(val => {
counter++;
var char = new RegExp(val);
if (char.test(str1) === false) { validate = false;} else
{
str1 = str1.slice(0, counter+1) + str1.slice(counter+1,str1.length);
console.log(str1);
}
});
return validate;
I think is not the most efficient though. Do you have a better solution for this?
Upvotes: 0
Views: 102
Reputation: 10466
Well this is my second answer, I might remove the previous answer later on, if I see this seems to be helpful for you.
Although there are some good answer being posted but yet as I can see that you want to have a hold on AND operation in regex, therefore you want a regexAND answer. Thus I am going to give you an answer that works according to your need. But if you want me to be frank, then I would say, for your requirement , regex operation is kind of the last option that I would want to go for.
Now comes the answer:
For each pair , say str1, and str2. You want to see if each character of str2 is present in str1.
thus you can make AND operation in the form of positive lookahead for each character for the entire string of str2 and see if all these exists in str1 or not. each positie lookahead would look likhe this: (?=.*w)
when written one beside another like the regex mentioned bellow they work as AND.
For example: str1="rkqodlw" str2="world"
make a regex by str2 in the following way:
^(?=.*w)(?=.*o)(?=.*r)(?=.*l)(?=.*d).*$
and see if that matches with str1
like this way:
function compare(str1,str2)
{
var regexAnd=str2.split("").join(")(?=.*");
var regexStr="^"+"(?=.*"+regexAnd+").*$";
console.log(regexStr); // prints the entire and condition
var re = new RegExp(regexStr,"gm");
return re.test(str1);
}
console.log(compare("scriptsjava","javascript"));
console.log(compare("jscripts","javascript"));
console.log(compare("rkqodlw","world"));
Upvotes: 0
Reputation: 522125
You want a set comparison, specifically a superset check. Simple with a Set
:
let s1 = 'javascript';
let s2 = 'scriptsjava';
Set.prototype.isSuperset = function(subset) {
for (var elem of subset) {
if (!this.has(elem)) {
return false;
}
}
return true;
}
console.log(new Set(s1).isSuperset(new Set(s2)));
Upvotes: 0