How to delete all text in the pattern of Special:[a-z]:?

In that webpage I wish to literally delete all words in the pattern of:

Special:[a-z]:

For example, Special:cool: or Special:hot: would have been deleted.

I have tried the following code but it didn’t work as I get “Undefined” texts in the document itself and the text of the pattern (Special:x:) remains:

const regex = /Special:[a-z]:/
const walker = document.createTreeWalker(
  document.body, 
  NodeFilter.SHOW_TEXT
)
let node;
while ((node = walker.nextNode())) {
  node.textContent = node.textContent.replace(regex);
  node.textContent = node.textContent.replace("");    
}

What have I done wrong?

reads as “The literal word Special, followed by a colon, followed by exactly 1 character in the range a-z, followed by a colon”.

You forgot to quantify your character class.

Thank you.

I think that the quantification here should be *.

I tried this which also didn’t work:

/Special:[A-Z][a-z]*:/

Now it says:
“The literal word Special, followed by a colon, followed by exactly 1 character in the range A-Z, followed by zero or more characters in the range a-z, followed by a colon”.
(Hint: Why does that sentence not match “Special:cool:”)

Indeed the example cool is bad, but in the linked webpage the names do indeed start with a capital letter, and yet, the code doesn’t work

:frowning:

well that’s incomplete, isnt it? surely replace takes two parameters at least; what to replace, and what to replace it with?

Yes it is incomplete.
The two commands confused me that there are two lines for the same command.

I think that my regex isn’t effective in rplace() as the following code doesn’t make the change:

const regex = /Special:[A-Z][a-z]*:/
const walker = document.createTreeWalker(
  document.body, 
  NodeFilter.SHOW_TEXT
)
let node;
while ((node = walker.nextNode())) {
  node.textContent = node.textContent.replace(regex, "EXPERIMENT");
}

If I change regex inside the replace() to Special:, than there is a change.

Can you show us the HTML you’re trying to modify?

Yes, it’s this Wikipedia webpage.

Right, so I can see where you’re confused, and why it’s not working.

Look at the ACTUAL HTML of those links you’re seeing on that page.

<li>
<a href="/wiki/Special:SpecialPages" title="Special:SpecialPages">Special:SpecialPages</a>
: a list of most special pages currently available to all users.
</li>

The text content of these nodes do not match your Regex. In fact, they fail it twice.
Why?

/Special:[A-Z][a-z]*:/
First of all, SpecialPages contains 2 capital letters; your pattern only allows for 1, at the beginning. The P in Pages will cause the regex not to match.
Secondly, the colon on the end. Note where the Anchor tag ends. It does not include the trailing colon. So it won’t match your pattern either. The trailing colon is not part of any textcontent of any of the anchors.

Okay so I guess I should use:

/Special:[A-Z][A-Za-z0-9]*:/

This is the first time I learn the term “trailing colon” :slight_smile: A Google search for trailing colon didn’t yield a general explanation so there probably is some context here.
You probably meant the last colon in the pattern but why wasn’t it matched?
Should I even match it due to the following whitespace character?

Your code looks through the HTML, and evaluates each node.

The node that contains the name of the link is:

<a href="/wiki/Special:SpecialPages" title="Special:SpecialPages">Special:SpecialPages</a>

There is no : at the end of the string inside that node. So it will never match, because it’s not there to be matched.

I understand, if I can match up to the colon it would be just wrong to put it in the [] package of what to match, okay, great :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.