Javascript regex match fails on actual page, but regex tests work just fine

You’re putting your regular expression inside a string. It should not be inside a string.

var world = document.documentElement.innerHTML.match(/boardid=[0-9]+">([A-Z][a-z]+)( - Trade){0,1}<\/a>/i)[1];

Another thing — it appears you have a document object, in which case all this HTML is already parsed for you, and you can take advantage of that instead of reinventing a fragile wheel.

var element = document.querySelector('a[href*="boardid="]');
var world = element.textContent;

(This assumes that you don’t need <=IE8 support. If you do, there remains a better way, though.)

(P.S. ? is shorthand for {0,1}.)

Leave a Comment