Javascript regex (continuous pattern repeats)

Let's say that I have a given string in javascript - for example, var s = "{{1}}SomeText{{2}}SomeText";It can be very long (for example, 25,000+ characters).

NOTE . I use "SomeText" here as a placeholder to refer to any number of characters in plain text. In other words, “SomeText” can be any text string that does not include {{1}} or {{2}}. So the above example may be var s = "{{1}}Hi there. This is a string with one { curly bracket{{2}}Oh, very nice to meet you. I also have one } curly bracket!";. And that would be perfectly correct.

The rules for him are simple:

No need to have instances {{2}}. However, if this happens, then after this instance we will not be able to meet another {{2}}, unless we find it {{1}}.

Valid examples:

"{{2}} SOMETEXT"

"{{1}} {{SOMETEXT 2}} SOMETEXT"

"{{1}} {SOMETEXT {1}} {{SOMETEXT 2}} SOMETEXT"

"{{1}} {SOMETEXT {1}} {{SOMETEXT 2}} {SOMETEXT {1}} SOMETEXT"

"{{1}} {SOMETEXT {1}} {{SOMETEXT 2}} {SOMETEXT {1}} {SOMETEXT {1}} SOMETEXT"

"{{1}} {SOMETEXT {1}} {{SOMETEXT 2}} {SOMETEXT {1}} {SOMETEXT {1}} {{SOMETEXT 2}} SOMETEXT"

etc...

Invalid examples:

"{{2}} {{SOMETEXT 2}} SOMETEXT"

"{{1}} {{SOMETEXT 2}} {{SOMETEXT 2}} SOMETEXT"

"{{1}} {{SOMETEXT 2}} {{SOMETEXT 2}} {SOMETEXT {1}} SOMETEXT"

etc...

- , - . , , " " - .

. , " ", ​​ .

+4
3

, . , {{2}}, , .

, , :

/{\{2}}([^{]*){\{2}}/

{{2}}, , {, {{2}}.

, { , regex {2} { (.. {{2} {),.


, {, {{2}}, , :

/{\{2}}((?!{\{1}}).)*{\{2}}/

{{2}}, , , {{1}}, {{2}}.

+4
(({{1}}SomeText)+({{2}}SomeText)?)*

:

({{1}} SomeText) + - 1 {{1}} ( )

({{2}} SOMETEXT)? - {{2}}

() *, 0 .

.

0

You said you can first have one instance of {2}, right?

^(.(?!{2}))(.{2})?(?!{2})((.(?!{2})){1}(.(?!{2}))({2})?)$

Note that if {2} is a single letter, replace all dots with [^ {2}]

0
source

All Articles