У меня проблемы с преобразованием текста в тег с регулярным выражением

У меня есть образец текста в моем div.content

These is your very first content with Contentful, pulled in JSON format using the [Content Delivery API](https://www.contentful.com/developers/docs/references/content-delivery-api/ "Content Delivery API").
Content and presentation are now decoupled, allowing you to focus your efforts in building the perfect app.
## Your first steps Building with Contentful is easy. First take a moment to get [the basics of content modelling](https://www.contentful.com/r/knowledgebase/content-modelling-basics/ "the basics of content modelling"), which you can set up in the [Contentful Web app](https://app.contentful.com/ "Contentful Web app").
Once you get that, feel free to drop by the [Documentation](https://www.contentful.com/developers/docs/ "Documentation") to learn a bit more about how to build your app with Contentful, in particular the [API basics](https://www.contentful.com/developers/docs/concepts/apis/ "API basics") and each one of our four APIs, as shown below.

Я хочу получить все такие строки:

[the basics of content modelling](https://www.contentful.com/r/knowledgebase/content-modelling-basics/ "the basics of content modelling")

из этого текста и замените их ссылкой a

и вставьте в теги html

<a href="https://www.contentful.com/r/knowledgebase/content-modelling-basics/ ">the basics of content modelling</a>

Я использую это регулярное выражение

let pattern = /\[(.*?)\]\((.*?)\)/gmi

person Rafał Bochniewicz    schedule 12.08.2019    source источник
comment
Если вы создадите JSFiddle и поделитесь этой ссылкой с нами, нам будет проще вам помочь.   -  person glend    schedule 12.08.2019


Ответы (1)


В вашем шаблоне отсутствует то, что () содержит как URL-адрес, так и строку в кавычках. Добавьте \s\".*?\" в свой шаблон - \s соответствует пробелу, вы можете опустить его, если хотите оставить пробел в конце URL-адреса

let pattern = /\[(.*?)\]\((.*?)\s\".*?\"\)/gm;

let text = `These is your very first content with Contentful, pulled in JSON format using the [Content Delivery API](https://www.contentful.com/developers/docs/references/content-delivery-api/ "Content Delivery API").
 Content and presentation are now decoupled, allowing you to focus your efforts in building the perfect app.


## Your first steps

Building with Contentful is easy. First take a moment to get [the basics of content modelling](https://www.contentful.com/r/knowledgebase/content-modelling-basics/ "the basics of content modelling"), which you can set up in the [Contentful Web app](https://app.contentful.com/ "Contentful Web app"). 
Once you get that, feel free to drop by the [Documentation](https://www.contentful.com/developers/docs/ "Documentation") to learn a bit more about how to build your app with Contentful, in particular the [API basics](https://www.contentful.com/developers/docs/concepts/apis/ "API basics") and each one of our four APIs, as shown below.`

let result = text.replace(pattern, '<a href="$2">$1</a>')

console.log(result)

person barbsan    schedule 12.08.2019
comment
Большое вам спасибо, это было не так уж и сложно, мне нужно узнать намного больше. - person Rafał Bochniewicz; 12.08.2019