-1

I want to match a pattern like this %data% in a string.

Although it can be easy but i need solution that can be robust for a big and multiline string.

For e.g.: i would like to match only the first and second matching %% combination and third and fourth (etc. etc.) in the following:

this %data% is not %data that i want to % show up in the results. % some more demo text %

I would like to match only the parts

%data%

,

%data that i want to %

,

% some more demo text %

from the above string..

I have tried:

I was trying to use this pattern but its selecting all of it from the first % to the last % symbol..

/^\%[\s\S]*\%$/gm

If this question has been asked and you want to close it.. just point me in the right direction and close it if you want.. thanks.

it is not the same question as marked by many.. with the title:

Regex Match all characters between two strings

and url: url as it is normal regular expression and not the one in JS (as JS doesn't support lookbehind) and it doesn't have the pair matching problem as mine.. hence, its completely different i think..


I would really like a good description of the RegEX if someone can provide it with the answer.. as i am not well versed in reading much complicated RegEx's, just getting used to it.. and i would also like an explanation of why my code is breaking and doesn't work to help me in future..

  • 1
    Possible duplicate of [Regex Match all characters between two strings](https://stackoverflow.com/questions/6109882/regex-match-all-characters-between-two-strings) – danieltakeshi Oct 30 '17 at 14:45
  • for this propouse you can use charAt for read each caracter and make conuter 0 start 1 end, to get text insde and position in string. IDe Twig https://github.com/twigphp/Twig/tree/2.x/lib/Twig – Álvaro Touzón Oct 30 '17 at 14:45
  • Try [regular expressions 101](https://regex101.com/r/CD1Oda/2) – Alan Larimer Oct 30 '17 at 14:51

2 Answers2

2

You can use the following regex:

/%(.*?)%/g

this lazily captures (i.e, as few times as possible, expanding as needed) everything between two %.

an alternative of this :

/%([^%]*)%/g

in this one, we're not matching lazily, instead we match (and capture) everything but a % after %

Working output using this regex :

const regex = /%(.*?)%/g;
const str = `this %data% is not %data that i want to % show up in the results. % some more demo text %`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}
Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39
  • `/%([^%]*)%/g` is more efficient – Tom Lord Oct 30 '17 at 14:51
  • 1
    It will, because if you exclude it from the match then you won't *skip* the characters between the 2nd and 3rd `%`. – Tom Lord Oct 30 '17 at 14:56
  • Hey, thanks. I understand and get that both are working and are good.. but can you tell me that, if i want to add the new line character in the match as well (as the "." dot character does not matches that), in this regex /%(.*?)%/g , how can i do that. and why doesn't adding \n inside the regular brackets like so /%([\n.]*?)%/gm doesn't work ? – Vimal__Frontend web Oct 30 '17 at 19:28
  • @Vimal__Frontendweb you can use : `[\s\S]` instead of `.` dot, to match even the new line character, `[\n.]` doesn't work because `.` doesn't have any special meaning when it's inside `[]`. see live demo here : https://regex101.com/r/dRPi5c/1 – Ashish Ranjan Oct 31 '17 at 03:24
1

This is what you need:

Regex:

(?<=\%)(.*?)(?=\%)

Input:

sdgsg%data%dgsgdsg

Output:

data

Marco Salerno
  • 5,131
  • 2
  • 12
  • 32