How can I replace bits or string with React component, react-router-dom Link or an anchor tag <a></a>

Zinox

How can I replace bits of string with links or components

This is a sample of the data

Ownership of Product/DiJUHl5zCE changed from Org/abcd to Org/efghi
Stage changed to completed for Payment of 254.15 gbp on Cycle Cycle/svgAjnox5t
Ownership of Product/DSGDHFHFDH changed from Org/sdef to Org/juhtr
Stage changed to completed for Payment of 4.15 gbp on Cycle Cycle/fdlkgdfkjdfl

I want to replace

Ownership of Product/DiJUHl5zCE changed from Org/dvju to Org/kijuy

Ownership of Product changed from OrgName to OrgName

Ownership of <a href="/product/Product/DiJUHl5zCE">Product</a> changed from <a href="/org/Org/abcd">OrgName</a> to <a href="/org/Org/efghi">OrgName</a>

and

Stage changed to completed for Payment of 254.15 gbp on Cycle Cycle/svgAjnox5t

Stage changed to completed for Payment of 254.15 gbp on Cycle Cycle

Stage changed to completed for Payment of 254.15 gbp on Cycle <a href="/cycle/Cycle/svgAjnox5t">Cycle</a>

I also have a regex

/([\w\d]+)\/([\w\d-]+)/g;

https://regex101.com/r/VrqlI7/1

that returns the groups but if I use replace() or replaceAll() the second argument returns string and in the browser it comes out as [object, object]

basically pass each line through a function and return a div with bits of text replaced with links

I am relatively new to react. any help much appriciated.

If I use `` at the start enter image description here enter image description here

if I use `` withing the href enter image description here enter image description here

How can I get this to display as links, Please advice

dmitryguzeev

You can use replaceAll with a callback

s.replaceAll(/([\w\d]+)\/([\w\d-]+)/g, (a) => {
  if (a.startsWith("Product")) { return `<a href="/product/${a}">${a}</a>` }
  else if (a.startsWith("Org")) { return `<a href="/org/${a}">${a}</a>` }
  else { return a }
})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I fix jsx-a11y/anchor-is-valid when using the Link component in React?

Usign react-router-dom, how can I access browserHistory object from outside a React component?

How can I change state before link works at react-router-dom

How to replace a react component with react-router

How, using react-router-dom, can I direct a link to render a general post component, populated with props?

How to replace catalog component with detailed item component using react router dom?

React Router Dom Link in another component

ReactJS: Go to anchor link in HTML with react router dom

How can I pass props from one component to another using Link and router in react

How to replace Link component behavior with History in React-Router

How can I pass props to a Link component from react-router-dom via Semantic UI's augmentation?

How do I select Link tag in React Router Dom with CSS?

React router dom not working with Link, but refreshing the page loads the component

How can i use the same behavior as react-router's Link on a function within functional component?

How can I pass the component's name from another component's object to Route in React Router Dom?

I have a problem with two .js files in a react app! I am using the react-router-dom. My problem is with the Link tag

How can I convert anchor elements to React Link components for routing?

React router anchor link

Type 'string' is not assignable to type for react-router-dom Link Component

I am passing the props to a functional component via Link (react-router-dom) but it says the state is 'undefined'

React App with React-Router-DOM: Link vs Achor Tag

How can I get a component to render as a parent of protected routes in react router dom?

I can not get the state from react router Link component using useLocation. So how can I pass it?

How I can check with react-router-dom 6.0.2 v

Error with the tag Link, react-router-dom

react-router-dom Link tag not rendering page

Misusing of <Link> tag in react-router-dom (the last version)

Interface for a React Button component that can be an anchor tag or a button tag

In React router dom can i pass a props in a Link?