Exploring Vue Routing with Regular Expressions

Recently, I integrated a route with a parameter in my Vue project.

const routes = [
  {
    path: "/:lang(^$|^es$|^pt$|^cn$)",
    name: "Home",
    component: Page,
  },
  {
    path: "/privacy",
    name: "Privacy",
    component: Privacy,
  },
  {
    path: "*",
    name: "NotFound",
    component: NotFound,
  }
];

I want the route to activate under specific conditions:

  1. If lang is empty
  2. If lang is es, pt, or cn, but not a combination of those

All other cases should redirect to the NotFound route

The regex I'm using has been tested on the JavaScript engine at https://regexplanet.com

Despite trying different variations, I haven't been successful in making it work properly within Vue yet.

Answer №1

It seems like the solution lies within this path:

path: "/:lang(|fr|de|it)",

If you want to test your routing, you can use a helpful tool that utilizes Vue's library:

Ensure that you are testing with version 0.1.7 of path-to-regexp.

The issue arises from the fact that the entire path is converted into a RegExp, not just the specific parts in the brackets. This means your ^ symbols will not match anything as expected. Use the testing tool to see how your original path is compiled. The generated RegExp already includes its own ^ and $, but they pertain to the entire path, not just the parameter being matched.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I configure my WordPress website to direct to index.html in the diste/templates directory rather than the default WordPress behavior?

I'm currently working on a headless WordPress website using Vue.js, and I've created a custom theme called vue-wordpress. The theme was set up using Vue CLI. Below is the layout of the directory: dist assets templates public src templates fun ...

The event listener for jQuery's document.ready doesn't trigger, rendering all jQuery functions ineffective

After researching similar issues on various forums, I have attempted the following troubleshooting steps: replacing all $ with jQuery ensuring the correct src is used implementing jQuery.noConflict() My goal is to retrieve data from a SQLite3 database w ...

Encountered a RunTime Error when attempting to Lazy Load a module

When attempting to lazy-load a component separately, an unexpected run-time error is occurring. This issue is specifically related to writing loadChildren within the routing module of another component in Angular 6. Any assistance with resolving this error ...

Axios consistently produces results in the form of strings but always seem to omit the final

I'm encountering an issue with my axios post method. public function viewAuthCheck(Request$request){ return [ 'test' => 'hello!' ]; } Here is my axios function: axios.post('/timetracking/settings/auth/check ...

Using Vue-Multiselect in conjunction with Laravel 5.3

I am relatively new to utilizing Laravel and Vue, and I require assistance with integrating Vue-Multiselect. I am unsure about how to transmit the actual options to the select component. Here is my Vue file: <template> <div class="dropdown" ...

How can I make my modal box appear after someone clicks on selecting a college?

Can someone help me figure out how to display my modal box after clicking into the selecting list? I've already coded it, but I'm struggling with getting it to show up after the click event. Any assistance is appreciated! In the image provided, ...

Learn how to transform a raw readme file into an HTML formatted document using AngularJS, after retrieving it from GitHub

Can someone help me figure out how to format each line of the README.MD raw file into an HTML document using the controller below? angular.module('ExampleApp', []) .controller('ExampleController', function($scope, Slim,$sce) { ...

Utilize AngularJS to modify the appearance of text

Just starting out with AngularJS and I need to achieve the following task. I have a text that says "bob" and two buttons, one for bold and one for italic. When the bold button is clicked, I want to make the text BOB bold and when the italic button is click ...

How can I send an error message from a Laravel controller and handle it in InertiaJS using the form helper?

I am facing an issue where I need to handle errors in the controller, but they are not Laravel validation errors. This prevents me from accessing them using the inertiajs form helper without using props.errors. How can I return the error in a way that allo ...

Exploring the wonders of retrieving JSON data in PHP through an Ajax request

A front-end developer is sending an array of data formatted as a JSON object using an Ajax call. The JSON object structure is shown below: { "name": " Test Name ", "image_url": "test URL", "include": [ "1" ], "dimension": [ null ], "media_type" ...

A guide on obtaining the date format according to locale using Intl.DateTimeFormat within JavaScript

Can someone assist me in obtaining the standard date format (such as MM/DD/YYYY) based on a specified local id? The code snippet provided below is not returning the desired format. Any guidance on how to achieve this would be greatly appreciated. var da ...

Tips for preventing the loss of ajax calls when an Oauth access-token expires

As the creator of a JavaScript browser application (SPA) that communicates with a server protected by OAuth 2, I encounter the challenge of using short-lived access tokens and longer-lived refresh tokens. While this specific scenario involves my own server ...

Creating dynamic components in ReactJS allows for versatile and customizable user interfaces. By passing

Within the DataGridCell component, I am trying to implement a feature that allows me to specify which component should be used to render the content of the cell. Additionally, I need to pass props into this component. Below is my simplified attempt at achi ...

Using JavaScript to alter CSS styles with dashes

Currently, I'm delving into the world of manipulating CSS using JavaScript: img.style.borderRadius = "100%"; img.style.border = "9px solid #ffffff"; img.style.boxShadow = "0 0 5px #00000070"; img.style.margin = "20px"; I'm wondering how to chan ...

Guide on implementing router-link within a select option dropdown in a Vue.js application

a.vue <template> <div>hi i am component 1</div> </template> <script> export default { name: "a", }; </script> b.vue <template> <div>hi i am component 2</div> </template> <script> ...

What is the proper way to parse an array of JSON objects?

Here is the data I need help with: var information = [ { "_id": "5e458e2ccf9b1326f11b5079", "xxTitle": "testtttttttttttt", "hhhhhhhhhh": "sssssssss", "xxzzzzzz": null, "oooooooooooooo": "ssssss", "xxDescription": "sssssss", "xxDetails": "ssssssss", "llll. ...

What is the best way to display a progress bar as a percentage?

Is there a way to visually represent the progress of an upload on the screen using percentages? I want to display a bar that starts at 0% and updates with the percentage from the percentComplete variable. Once the upload is finished, I'd like to show ...

Experiencing difficulties when attempting to launch a React application and Express/Node.js backend on a shared port

I'm trying to set up my react front-end on localhost:5000, with API requests going to localhost:5000/api/some-path. After looking at similar questions, I've come to understand the following: Include a proxy in the package.json file Serve st ...

What is the best way to integrate custom JavaScript files into a Vue project?

I recently downloaded an HTML template from a website and now I am looking to convert the entire template into a Vue CLI project. The template includes jQuery and other custom JavaScript files. While I was able to use npm packages for jQuery and Bootstrap, ...

Tips for Elevating State with React Router Version 6

Looking for advice on sharing state between two routes in my project. I'm debating whether to lift state up from my AddContact component to either the Layout or App components in order to share it with the ContactList. The Layout component simply disp ...