The simplest regex pattern to do this is /<a href="([^>]*)">(.+)<\/a>/.
Test example:
console.clear()
const parseVueLinks = ($value) => {
const re = /<a href="([^>]*)">(.+)<\/a>/g;
const matches = re.exec($value);
return `<router-link :to="{ path: '${matches[1]}'}">${matches[2]}</router-link>`
}
console.log(parseVueLinks('<a href="/about-us">Text</a>'))
console.log(parseVueLinks('<a href="http://google.com">Goooooogle</a>'))
I don't know PHP, but am guessing the equivalent PHP might be (tested at https://www.functions-online.com/preg_match.html):
function parseVueLinks($value)
{
$pattern = "/<a href="([^>]*)">(.+)<\/a>/";
$matches = [];
preg_match($pattern, $replace, $matches);
return "<router-link :to=\"{ path: '" + $matches[1] + "'}\">" + $matches[2] + "</router-link>";
}
I'm wondering about the presence of http|https|mailto|tel in your regex, does this mean you want to do some validation on the link?
If so, using preg_match() allows a second regex step to be performed on $matches[1] before output. It would seem simpler to validate as a second step rather than using one big regex.
Edit following comment
The issue is not in regex. It is in Vue not parsing content pulled from the server
This may not apply if you're using server side rendering, but this is how I apply links from content.
MyComponent.ts
<template>
<div class="row">
...
<router-link :to="'/' + measure.link" class="measure">
<i class="measure-icon fa fa-lg" :class="measure.icon" aria-hidden="true">
<span class="title">{{measure.title}}</span>
</i>
</router-link>
Here, measure is an object that is fetched from the server. I you are fetching the full <router-link>, that may work with a Dynamic Component but it seems like overkill, since you know the element will be a <router-link.
Note, if there's also a problem with the server responding with 404 to the click, you can use hash-mode routing (default) by adding # before the link, e.g #/about-us.
Alternatively, set history mode in the Vue router.
const router = new Router({
routes,
mode: 'history'
})
This requires the server to redirect to index.html for a 404. See HTML History Mode.
Also, you then need to handle 404 in Vue with a catch-all route,
const routes = [
...
{ path: '*', component: NotFoundComponent },
]