Regular expression to get a string between two strings in Javascript

前端 未结 11 2163
萌比男神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 07:09

    I was able to get what I needed using Martinho Fernandes' solution below. The code is:

    var test = "My cow always gives milk";
    
    var testRE = test.match("cow(.*)milk");
    alert(testRE[1]);
    

    You'll notice that I am alerting the testRE variable as an array. This is because testRE is returning as an array, for some reason. The output from:

    My cow always gives milk
    

    Changes into:

    always gives
    

提交回复
热议问题