0

I have three string and i need to check if one of them contains the other 2 in the same order given, right now i am using alot of conditions to achive this, is there a way to simplify it using regex?

here is the code:

var a = '**'; var b = '##'; var phrase = '**Lorem ipsum dolor sit amet##'; if(phrase.includes(a) && phrase.includes(b) && (phrase.indexOf(a) < phrase.indexOf(b))) { // add logic } 
4
  • Please may you share regexes you have tried? Commented Nov 21, 2021 at 15:01
  • Does this answer your question? Regex Match all characters between two strings Commented Nov 21, 2021 at 15:09
  • @RobertHarvey That duplicate will not answer the question. Commented Nov 21, 2021 at 15:11
  • I don't even know what the question is. Commented Nov 21, 2021 at 17:06

1 Answer 1

2

You could use a pattern to match ** before ##

^(?:(?!##|\*\*).)*\*\*.*## 
  • ^ Start of string
  • (?:(?!##|\*\*).)* Match any char while not directly followed by either ## or **
  • \*\* First match **
  • .* Match any character 0+ times
  • ## Match ##

Regex demo

var phrase = '**Lorem ipsum dolor sit amet##'; if (phrase.match(/^(?:(?!##|\*\*).)*\*\*.*##/)) { // add logic } 

var phrases = [ '**Lorem ipsum dolor sit amet##', 'test****#**##', '##**Lorem ipsum dolor sit amet##', '##**##**', ]; phrases.forEach(phrase => { if (phrase.match(/^(?:(?!##|\*\*).)*\*\*.*##/)) { console.log(`Match for ${phrase}`); } else { console.log(`No match for ${phrase}`); } });

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

3 Comments

What about something simpler /\*\*.+?##/?
@evolutionxbox Because that something simpler gives a false positive here regex101.com/r/IO4n0u/1
@Thefourthbird thanks thats what i was looking for

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.