Wordle regex solver by Sketch Engine

Helps you solve a Wordle puzzle by generating a regular expression. To look up possible solutions in a word list, Sketch Engine subscription is required (30-day free trial available, no card required).

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Wordle regex solver by Sketch Engine
// @namespace    https://www.sketchengine.eu/
// @version      0.9
// @description  Helps you solve a Wordle puzzle by generating a regular expression. To look up possible solutions in a word list, Sketch Engine subscription is required (30-day free trial available, no card required).
// @author       Marek Blahuš of Lexical Computing
// @match        https://www.nytimes.com/games/wordle/index.html
// @icon         https://www.nytimes.com/games/wordle/images/NYT-Wordle-Icon-32.png
// @grant        none
// ==/UserScript==

/* jshint esversion: 6 */

var board = document.getElementsByTagName('game-app')[0].shadowRoot.getElementById('board');
board.innerHTML += '<div style="text-align: center"><a href="#" id="wordle-regex-ske-link">need help?</a></div>';
board.getElementById('wordle-regex-ske-link').addEventListener('click', wordle_regex_ske);

function wordle_regex_ske(){
  var absents='', presents='', incorrects=Array(5).fill(''), corrects=Array(5).fill('.');
  for(var d of Array.from(Array(6), (_, i) => document.querySelectorAll('game-app')[0].boardState[i] ? Array.from(Array(5), (_, j) => [document.querySelectorAll('game-app')[0].boardState[i][j], document.querySelectorAll('game-app')[0].evaluations[i][j], j]) : []).flat()) {
    switch(d[1]) {
      case "absent": if(!absents.includes(d[0])) absents += d[0]; break;
      case "present": if(!corrects.includes(d[0])) { if(!presents.includes(d[0])) presents += d[0]; if(!incorrects[d[2]].includes(d[0])) incorrects[d[2]] += d[0]; } break;
      case "correct": corrects[d[2]] = d[0]; presents = presents.replace(d[0], ''); incorrects = incorrects.map((v) => v.replace(d[0], '')); break;
      }
  }
  var regex = (absents && corrects.includes('.') ? '(?!.*'+(absents.length>1?'['+absents+']':absents)+')' : '') + (presents.split('').map((v) => '(?=.*'+v+')').join('')) + (incorrects.map((v, i) => v ? '(?!'+'.'.repeat(i)+(v.length>1?'['+v+']':v)+')' : '').join('')) + (corrects.join(''));
  if(confirm('Your regex is:\n'+regex+'\nProceed to Sketch Engine to see a word list? (Login required, free trial available.)')) window.open('https://app.sketchengine.eu/#wordlist?corpname=preloaded%2Fcoca&tab=advanced&keyword='+encodeURIComponent(regex)+'&filter=regex&wlattr=lc&wlminfreq=0&showresults=1&cols=%5B%22frq%22%5D');
}