简体   繁体   English

如何将通配符字符串与javascript进行比较

[英]how to compare wildcard string with javascript

I want to compare two strings one being wildcard with javascript.我想比较两个字符串,一个是通配符与 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. <div class="Modal A2aModalStep Step">并在变量 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...但问题是类名会随机改变它的位置,就像有时step Model A2aModalStepA2aModalStep Step Model等......

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.我在 imacros 中使用这个 javascript,所以不能使用预定义的函数。 must be in pure javascript comparison using operator and variable必须在使用运算符和变量的纯 javascript 比较中

If you have access to the webpage itself, use DOM methods to look for if an element with those 3 classes exists:如果您有权访问网页本身,请使用 DOM 方法查找具有这 3 个类的元素是否存在:

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 :如果您无法使用任何 DOM 方法,我想您可以改用正则表达式:在匹配" ,先行查找[^"]*\\bModal\\b[^"]*\\bA2aModalStep ,以及对于[^"]*\\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\\">"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM