0

I'm witing a function that returns a HTML class according to the last part of the url:

const currentPage = this.$route.path
const pinkPages = [ '/', '/forgot-pass', '/reset-pass' ]
if (pinkPages.indexOf(currentPage) > -1) return 'footer-pink'

It work fine, the problem is that /reset-pass actually has more characters at the end. For example, /reset-pass/123.

How can I make it so that the function returns footer-pink when the URL is /reset-pass/1234 or /reset-pass/abcd or other pattern like that?

4
  • 1
    can you explain "How can I include that to the pinkPages array, so 'footer-pink` is returned when the URL matches that?" more? Commented May 12, 2016 at 6:25
  • @KhalidHabib I edited the question. Commented May 12, 2016 at 6:33
  • instead of adding regex in array, you can create dynamic regex using new RegExp(). Commented May 12, 2016 at 6:38
  • @alex, can you show me the current currentPage value? Commented May 12, 2016 at 6:44

2 Answers 2

1

Maybe you want to use a regex to try to match with current page ?

console.log(matchWith('/forgot-pass'));
console.log(matchWith('/reset-pass/'));
console.log(matchWith('/reset-pass/toto'));
console.log(matchWith('/another'));

function matchWith(page) {
  var regex = /(^\/$|\/forgot-pass$|\/reset-pass\/(.*))/
    if(page.match(regex))
      return true
    return false
}
// for your sample
if(matchWith(this.$route.path)) {
   return 'footer-pink';
}

Look this plunker, maybe it's your answer : https://plnkr.co/edit/sC5VCadDLlRJwH6rpB1T?p=preview

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. That was a very simple and effective solution.
1

You can store regex in string format and then create an array of regex.

var arr = ["test", "foo", "test2", "bar", "test3"];
var reg_str = ["^test", "o$"];
var regex_list = reg_str.map(function(r) {
  return new RegExp(r);
})

var filtered_list = arr.filter(function(item) {
  return regex_list.filter(function(r) {
    return r.test(item);
  }).length > 0;
});

console.log(filtered_list);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.