1

I have the following string.

**Template:**
```
**label**: test
**label2**: test
**label3**: test
```

Inside of the `````` has "labels" and with regex i'd like to select those labels:

[label1, label2, label3]

And then check another string that is similar,

**Template:**
```
**label**: Something here
**label2**: Something there.
**label3**: Something everywhere.
```

Check if the string above, contains everything that the array has. The ** aren't important, but it would be nice to check if it has ** too (but in a separate method)

What I've tried:

([/\*(*?)\*/])\w+

It selects the text but also the asterisks behind the text. *label, and also doesn't account that I need to select everything that's between the codeblock.

How can I do this?

kinx
  • 463
  • 5
  • 12
  • 31
  • 1
    Yes, I've tried other solutions from answers such as where regex is used for selecting text for html, and it didn't seem to work, but close. I'll edit the question to show what I've tried. – kinx Jan 19 '18 at 05:18
  • 1
    You could use: `\*{2}(.*)\*{2}` to extract the labels. https://regex101.com/r/aHUj37/1 The answer [here](https://stackoverflow.com/a/432503/5894241) shows how you can extract the text from capturing groups. – Nisarg Shah Jan 19 '18 at 05:22
  • Nice, thanks @NisargShah, what about only if it's in the ``` codeblock? – kinx Jan 19 '18 at 05:35
  • So, to be clear, you don't want to select `**Template**` but only the content that is wrapped inside three backticks (```). Is that correct? – Nisarg Shah Jan 19 '18 at 05:38
  • That is correct. – kinx Jan 19 '18 at 05:42

1 Answers1

1

I'm using two regexes here.

First:

/\`{3}([\s\S]*)\`{3}/g

To get the blocks of code, i.e. the content wrapped inside the (```) three backticks.

Second:

/\*{2}(.*)\*{2}/gm

To get the labels from code block, i.e. the content wrapped inside the (***) two stars.

Also note that the code in snippet below assumes that there is only one code block, which can contain multiple labels. If you're dealing with multiple code blocks, you can handle that scenario using a while (matchCode ) { ... } section, and then searching for labels within each of them.

var str = document.getElementById("myDiv").innerText;

// console.log(str);

var labels = [];

// Get the content between the ``` backticks
var regexCode = /\`{3}([\s\S]*)\`{3}/g;

// Note: Generalize this if you expect there to be multiple blocks of code.
var matchCode = regexCode.exec(str);
var codeText= matchCode[1];
console.log("Content inside code block: ", codeText);

// Get the labels.
var regexLabels = /\*{2}(.*)\*{2}/gm;
var matchLabel = regexLabels.exec(codeText);
while(matchLabel) {
  labels.push(matchLabel[1]);

  matchLabel = regexLabels.exec(codeText);
}

console.log("Labels: ", labels);
<pre id="myDiv">
**Template:**
```
**label**: test
**label2**: test
**label3**: test
```
</pre>
Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55