The simplest regex pattern for 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 do not know PHP, but I assume that the equivalent PHP can 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 am interested to know about the presence of http|https|mailto|tel in your regular expression, does this mean that you want to do some verification in the link?
If so, using preg_match() allows you to perform the second step of the regular expression on $matches[1] before exiting. It would seem easier to check as a second step, rather than using one large regular expression.
Edit next comment
The problem is not regular expression. It is in Vue that content pulled from the server does not understand
This may not apply if you are using server-side rendering, but this is how I apply links from content.
Mycompponent.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 retrieved from the server. I get a full <router-link> that can work with the Dynamic component , but it seems redundant since you know the element will be <router-link .
Please note that if there is a problem with the server responding with 404 , you can use hash mode routing (default) by adding # before the link, for example #/about-us .
Alternatively, set the history mode in the Vue router.
const router = new Router({ routes, mode: 'history' })
This requires the server to be redirected to index.html for 404. See HTML History Mode .
In addition, you need to handle 404 in Vue using the catch-all route,
const routes = [ ... { path: '*', component: NotFoundComponent }, ]