Search code examples
javascriptimacros

how to compare wildcard string with javascript


I want to compare two strings one being wildcard with javascript.

I'm checking whether this string is found on web page or not.

<div class="Modal A2aModalStep Step"> and retrieving it in variable k.

and want to compare it.

like

if ( k == "<div class=\"Modal A2aModalStep Step\">") {
  //some code
} 

But the problem is that class name changes it's pos randomly like sometimes step Model A2aModalStep or A2aModalStep Step Model etc...

so, I want to compare

if ( k == "<div class=\"[Model|A2aModalStep|Step] [Model|A2aModalStep|Step] [Model|A2aModalStep|Step]\">"){
  //some code
} 

I'm using this javascript in imacros, so can't use predefined function. must be in pure javascript comparison using operator and variable


Solution

  • If you have access to the webpage itself, use DOM methods to look for if an element with those 3 classes exists:

    const div = document.querySelector('.Modal.A2aModalStep.Step');
    if (div) {
      // some code
    }
    

    Selector strings don't care about the order of classes.

    If you're not able to use any DOM methods, I suppose you could use a regular expression instead: after matching the ", lookahead for [^"]*\bModal\b, and for [^"]*\bA2aModalStep, and for [^"]*\bStep:

    const check = str => /div class="(?=[^"]*\bModal)(?=[^"]*\bA2aModalStep)(?=[^"]*\bStep)/.test(str);
    console.log(check("<div class=\"Modal A2aModalStep Step\">"));
    console.log(check("<div class=\"Modal Step A2aModalStep\">"));
    console.log(check("<div class=\"Modal A2aModalStep\">"));