Includes test fails on an array of strings

I am trying to loop through an array of strings this way:

window.setInterval( () => {
    const urlPatternToBlock = [
        'x' ||

    for (let element of urlPatternToBlock) {
        if ( window.location.href.includes(urlPatternToBlock) ) {
  "", "_self");
}, 1);

The first string in the array is read successfully.

The second string in the array isn’t.

What may cause this situation?

That is not how you define an array of values. Values in the array are separated by a comma, not ||.

What you’ve done is create an array with only a single value in it, which is 'x'.


Yeah maybe you’re confusing the logical disjunction with a regular expression disjunction here? This would work too:

const urlPatternToBlock = /x|y/

if (urlPatternToBlock.test(window.location.href)) {'')

I understand.

I actually tried comma before but a had another problem that mislead me to think that the comma actually says “this, and this” instead, “this, then this”.

Using the /x|y/ regex pattern is interesting. The only problem I have with this is that it’s single-lined and I prefer something multi lined (this is very short but if it becomes much larger, it’s a problem for me personally).

Or can it be multi lined as well?

If you have a complex regular expression and want to split it over multiple lines for readability, you can’t do it directly in JavaScript because regular expressions don’t allow line breaks.

However, you can construct the regular expression from a string and use the RegExp constructor. When you use a string to create the regular expression, line breaks and white spaces are allowed.

Here’s how you can do it:

const regex = new RegExp(
  'x|' +

console.log(regex.test('x')); // true
console.log(regex.test('y')); // true
console.log(regex.test('z')); //false

Or, to come full circle… :-P

const regex = new RegExp([

// ...

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