Home > database >  Capture matching text format in a string
Capture matching text format in a string

Time:01-26

I am trying to write a regex to extract the items in the text below that start with the # and ends with )

const bodyOfText = "#[DataStructures](topic_DataStructures) is one #[Algorithms](topic_Algorithms) branch that could #[Make or Mar](topic_Make or Mar)";

So basically, will want an array that looks like:

["#[DataStructures](topic_DataStructures)", "#[Algorithms](topic_Algorithms)", "#[Make or Mar](topic_Make or Mar)"]

CodePudding user response:

Using string match() we can try:

var bodyOfText = "#[DataStructures](topic_DataStructures) is one #[Algorithms](topic_Algorithms) branch that could #[Make or Mar](topic_Make or Mar)";
var matches = bodyOfText.match(/#\[.*?\]\(.*?\)/g);
console.log(matches);

  •  Tags:  
  • Related