Regular expression to get a string between two strings in Javascript

前端 未结 11 1983
萌比男神i
萌比男神i 2020-11-21 06:29

I have found very similar posts, but I can\'t quite get my regular expression right here.

I am trying to write a regular expression which returns a string which is b

11条回答
  •  太阳男子
    2020-11-21 06:53

    I find regex to be tedious and time consuming given the syntax. Since you are already using javascript it is easier to do the following without regex:

    const text = 'My cow always gives milk'
    const start = `cow`;
    const end = `milk`;
    const middleText = text.split(start)[1].split(end)[0]
    console.log(middleText) // prints "always gives"
    

提交回复
热议问题