0

I am using JavaScript and would like to match everything in a custom template language like this:

Begin10
    Match THIS keyword and ANOTHER
End10

So I would like to find Begin10 using the 10 as variable to find End10 and match THIS and ANOTHER between them. I've looked at capture groups. I assume this is the way to go, but I can't figure out how to compose the expression.

THIS and ANOTHER need to be targeted for syntax highlighting by my code.

3 Answers 3

2

You can use regex with captured group

var str = `Begin10
    Match THIS keyword and ANOTHER1
End10

Begin20
    Match THIS keyword and ANOTHER2
End20`;

console.log(
  str.match(/\bBegin(\d+)[\s\S]*?\bEnd\1\b/g)
);

To get the string between them, do something like this

var str = `Begin10
    Match THIS keyword and ANOTHER1
End10

Begin20
    Match THIS keyword and ANOTHER2
End20`;

var res = [],
  regex = /\bBegin(\d+)\s+([\s\S]*?)\s+\bEnd\1\b/g,
  match;

while (match = regex.exec(str)) {
  res.push(match[2]);
}

console.log(res);

Regex explanation here

Regular expression visualization


UPDATE :

If there is only THIS or ANOTHER between them then use

var str = `Begin10
    THIS
End10

Begin20
    ANOTHER
End20`;

var res = [],
  regex = /\bBegin(\d+)\s+(THIS|ANOTHER)\s+\bEnd\1\b/g,
  match;

while (match = regex.exec(str)) {
  res.push(match[2]);
}

console.log(res);

Sign up to request clarification or add additional context in comments.

6 Comments

I am trying to replace [\s\S] in the expression so that I can target THIS and ANOTHER instead. I don't wish to match the whole string
can you give an example
I'd assume a small change like this would work, /\bBegin(\d+)\s+(THIS|ANOTHER)\s+\bEnd\1\b/g
@Ade : only these two strings between them??
Yes, I first want to find Begin(\d+) and match it with (End\1) but then I need to search between these strings and find THIS and ANOTHER only.
|
1

This will work

\bBegin(\d+)\b([\S\s]*)\bEnd\1\b

Regex Demo

Comments

0
\nBegin(\d+)\s*\nMatch \b(\w+)\b keyword and \b(\w+)\b\s*\nEnd\1\n

Explanation:

  1. \n will match a newline, and I am sure you don't want to get anything besides newline.
  2. \1 that's a variable for the first group to make sure it has the same value.
  3. \s* just to ignore some unnecessary whitespaces.

5 Comments

Actually, if you write a good regex, it does not have to be non-greedy.
But it's a must in this question. I don't think there is other alternative in this case.
If you do not know a better alternative, it does not mean there is none. Study unroll the loop technique to write better regexps.
I know. Sure! Thank you for letting me know. It's nice to get advanced knowledge in StackOverflow
I wish to target THIS and ANOTHER so I can highlight them as keywords. The answers here have already taken me halfway through

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.