FooBar
FooBar

Reputation: 6128

Replace <a> link with <nuxt-link> in string variable

I retrieve content from a CMS system (WordPress). In the "page.content" variable, links are outputted like so: <a href="#">. How can I replace this content with: <nuxt-link to="#"> on the client side?

The question has been asked before, but no real answers: https://github.com/nuxt/nuxt.js/issues/2912 - despite this must be a pretty regular usecase.

Upvotes: 1

Views: 2357

Answers (1)

GBWDev
GBWDev

Reputation: 596

Simple string replacement should suffice.

const string =  'links are outputted like so: <a href="#">. How can I replace this content with: <nuxt-link to="#">'

const converted = string.replace(/<a/g, '<nuxt-link').replace(/href=/g, 'to=');

console.log(converted)

Upvotes: 2

Related Questions