2

When removing a list item based on the browsers width I can do:

$(document).ready(function() {
    var changeWidth = 992;
    if ($(window).width() < changeWidth) {
        $('li:contains("Home")').remove();
    }
});

When researching for a way to remove Home with a typical regex pattern similar to Home|HOME I did a search and learned about RegExp so I tried:

$(document).ready(function() {
    var changeWidth = 992,
        regex = new RegExp('Home|HOME');

    if ($(window).width() < changeWidth) {
        $('li:contains("' + regex + '")').remove();
    }
});

it didn't work so I researched and found Javascript RegExp: Create regex with variable [duplicate] and tried:

$(document).ready(function() {
    var changeWidth = 992,
        regex = new RegExp("Home", "i");

    if ($(window).width() < changeWidth) {
        $('li:contains("' + regex + '")').remove();
    }
});

after that didn't work I found this and tried:

$(document).ready(function() {
    var changeWidth = 992,
        pattern1 = "Home",
        pattern2 = "HOME",
        regex = new RegExp(pattern1+'|'+pattern2, 'gi');

    if ($(window).width() < changeWidth) {
        $('li:contains("' + regex + '")').remove();
    }
});

After doing further reading from:

I tried:

$(document).ready(function() {
    var changeWidth = 992,
        string = "Home",
        regex = new RegExp("HOME", 'gi'),
        result = string.match(regex);

    if ($(window).width() < changeWidth) {
        $('li:contains("' + result + '")').remove();
    }
});

it works if Home is present but not when HOME is present. What am I doing wrong and how can I regex for Home and HOME?

DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127

0 Answers0