Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9171957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:14:03+00:00 2026-06-17T16:14:03+00:00

I have a very specific problem concerning a regular expression matching in Javascript. I’m

  • 0

I have a very specific problem concerning a regular expression matching in Javascript. I’m trying to match a piece of source code, more specifically a portion here:

<TD WIDTH=100% ALIGN=right><a href="http://forum.tibia.com/forum/?action=main&amp;sectionid=2">World Boards</a> | <a href="http://forum.tibia.com/forum/?action=board&amp;boardid=106121">Olympa - Trade</a> | <b>Bump when Yasir...</b></TD>

The part I’m trying to match is boardid=106121">Olympa - Trade</a>, the part I actually need is “Olympa”. So I use the following line of JS code to get a match and have “Olympa” returned:

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

the ( - Trade) part is optional in my problem, hence the {0,1} in the regex.

There’s also no easier way to narrow down the code by e.g. getElementsByTagName, so searching the complete source code is my only option.

Now here’s the funny thing. I have used two online regex matchers (of which one was for JS-regex specifically) to test my regex against the complete source code. Both times, it had a match and returned “Olympa” exactly as it should have. However, when I have Chrome include the script on the actual page, it gives the following error:

Error in event handler for 'undefined': Cannot read property '1' of null TypeError: Cannot read property '1' of null

Obviously, the first part of my line returns “null” because it does not find a match, and taking [1] of “null” doesn’t work.

I figured I might not be doing the match on the source code, but when I let the script output document.documentElement.innerHTML to the console, it outputs the complete source code.

I see no reason why this regex fails, so I must be overlooking something very silly. Does anyone else see the problem?

All help appreciated,
Kenneth

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:14:03+00:00Added an answer on June 17, 2026 at 4:14 pm

    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}.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very specific problem. I have a user control (code below) That
I have a very specific problem. I would like to insert a specific code
The problem that I have is somehow very specific. I have to implement a
I have very simple piece of code. The goal is when i input four-digit
I have a very specific problem in T-SQL. If I can solve this example
I have a very specific problem here. I have a multi-dimensional array that I
i have a very specific problem with the mapping of an xml file to
I'm trying to find some very specific data in a string. The problem is
I have a very specific problem which don't seem to be asked before -
I have very specific question, I cannot find a problem. I try update some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.