252

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 between two other strings. For example: I want to get the string which resides between the strings "cow" and "milk".

My cow always gives milk

would return

"always gives"

Here is the expression I have pieced together so far:

(?=cow).*(?=milk)

However, this returns the string "cow always gives".

Dexygen
  • 12,287
  • 13
  • 80
  • 147
phil
  • 3,538
  • 2
  • 24
  • 24
  • 7
    I stumbled on this old question and wanted to clarify why testRE is an array. test.match returns an array with first index as the total match (therfor, the string that matches cow(.*)milk) and then, all the trapped strings like the (.*) if there was a second set of parenthesis they would then be in testRE[2] – Salketer Mar 06 '13 at 15:16
  • 5
    This solution will not work if you are searching over a string containing newlines. In such a case, you should use "STRING_ONE([\\s\\S]*?)STRING_TWO". http://stackoverflow.com/questions/22531252/js-regex-to-match-all-characters-including-newline-between-two-strings-but-wit – Michael.Lumley Sep 30 '14 at 21:36
  • 1
    just for reference the match method on MDN https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/match – vzR Mar 28 '17 at 13:19

13 Answers13

248

A lookahead (that (?= part) does not consume any input. It is a zero-width assertion (as are boundary checks and lookbehinds).

You want a regular match here, to consume the cow portion. To capture the portion in between, you use a capturing group (just put the portion of pattern you want to capture inside parenthesis):

cow(.*)milk

No lookaheads are needed at all.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
  • 52
    When I test this, the provided Regex expression includes both "cow" and "milk"... – TheCascadian Apr 27 '18 at 03:39
  • 11
    This is missing a step. When you get the result of the match, you need to extract the matched text of the first capturing group with `matched[1]`, not the whole matched text with `matched[0]`. – Rory O'Kane Jun 20 '18 at 20:57
  • 14
    In Javascript, you actually need to use `([\s\S]*?)` rather than `(.*?)`. –  Dec 18 '18 at 11:07
  • 18
    Although this is a useful techique, it was downvoted because IMHO this is NOT the right answer for the question, since it includes "cow" and "milk", as stated by @TheCascadian – Almir Campos Feb 20 '19 at 21:54
  • 1
    @AlmirCampos - if I am not mistaken there is no way to do this match without matching "cow" and "milk" (since you want to match what's in between those two). The problem is not in the RegEx itself but how you handle it afterwards (as mentioned by Rory O'Kane). Otherwise you could only match for surrounding spaces - and that would give you a VERY wrong return, wouldn't it? – sborn May 11 '20 at 19:23
  • 3
    @sborn - Thanks for pointing this out. I think the question gives room for interpretations. What I have in mind is a (vanilla - as much as possible) regex that filters the original message and provides the result asked. It would be the case of this regex: `/([^(my cow)])(.*)[^(milk)]/g` Please, check the fiddle at https://jsfiddle.net/almircampos/4L2wam0u/5/ and let us know your thoughts. – Almir Campos May 12 '20 at 05:44
  • 2
    Just in case others are reading this, THIS RETURNS "cow always gives milk" NOT "always gives". Now read, `sborn`'s comment. – Tigerrrrr Nov 15 '20 at 15:49
  • Can anyone help me with something similar. I need a regex to extract a string from a url which is between personal/ and the next immediate /. Example I need the my_id from the below url `htttps://www.abc.com/personal/my_id/hello/randomstring`. I used `/(?<=personal\/).*?(?=\/)/` but somehow it does not work in Safari. Please help – Treesa Jul 19 '22 at 09:25
  • this will not work with this string: `only my cow always gives milk and more milk` – MeSo2 May 30 '23 at 14:44
141

Regular expression to get a string between two strings in JavaScript

The most complete solution that will work in the vast majority of cases is using a capturing group with a lazy dot matching pattern. However, a dot . in JavaScript regex does not match line break characters, so, what will work in 100% cases is a [^] or [\s\S]/[\d\D]/[\w\W] constructs.

ECMAScript 2018 and newer compatible solution

In JavaScript environments supporting ECMAScript 2018, s modifier allows . to match any char including line break chars, and the regex engine supports lookbehinds of variable length. So, you may use a regex like

var result = s.match(/(?<=cow\s+).*?(?=\s+milk)/gs); // Returns multiple matches if any
// Or
var result = s.match(/(?<=cow\s*).*?(?=\s*milk)/gs); // Same but whitespaces are optional

In both cases, the current position is checked for cow with any 1/0 or more whitespaces after cow, then any 0+ chars as few as possible are matched and consumed (=added to the match value), and then milk is checked for (with any 1/0 or more whitespaces before this substring).

Scenario 1: Single-line input

This and all other scenarios below are supported by all JavaScript environments. See usage examples at the bottom of the answer.

cow (.*?) milk

cow is found first, then a space, then any 0+ chars other than line break chars, as few as possible as *? is a lazy quantifier, are captured into Group 1 and then a space with milk must follow (and those are matched and consumed, too).

Scenario 2: Multiline input

cow ([\s\S]*?) milk

Here, cow and a space are matched first, then any 0+ chars as few as possible are matched and captured into Group 1, and then a space with milk are matched.

Scenario 3: Overlapping matches

If you have a string like >>>15 text>>>67 text2>>> and you need to get 2 matches in-between >>>+number+whitespace and >>>, you can't use />>>\d+\s(.*?)>>>/g as this will only find 1 match due to the fact the >>> before 67 is already consumed upon finding the first match. You may use a positive lookahead to check for the text presence without actually "gobbling" it (i.e. appending to the match):

/>>>\d+\s(.*?)(?=>>>)/g

See the online regex demo yielding text1 and text2 as Group 1 contents found.

Also see How to get all possible overlapping matches for a string.

Performance considerations

Lazy dot matching pattern (.*?) inside regex patterns may slow down script execution if very long input is given. In many cases, unroll-the-loop technique helps to a greater extent. Trying to grab all between cow and milk from "Their\ncow\ngives\nmore\nmilk", we see that we just need to match all lines that do not start with milk, thus, instead of cow\n([\s\S]*?)\nmilk we can use:

/cow\n(.*(?:\n(?!milk$).*)*)\nmilk/gm

See the regex demo (if there can be \r\n, use /cow\r?\n(.*(?:\r?\n(?!milk$).*)*)\r?\nmilk/gm). With this small test string, the performance gain is negligible, but with very large text, you will feel the difference (especially if the lines are long and line breaks are not very numerous).

Sample regex usage in JavaScript:

//Single/First match expected: use no global modifier and access match[1]
console.log("My cow always gives milk".match(/cow (.*?) milk/)[1]);
// Multiple matches: get multiple matches with a global modifier and
// trim the results if length of leading/trailing delimiters is known
var s = "My cow always gives milk, thier cow also gives milk";
console.log(s.match(/cow (.*?) milk/g).map(function(x) {return x.substr(4,x.length-9);}));
//or use RegExp#exec inside a loop to collect all the Group 1 contents
var result = [], m, rx = /cow (.*?) milk/g;
while ((m=rx.exec(s)) !== null) {
  result.push(m[1]);
}
console.log(result);

Using the modern String#matchAll method

const s = "My cow always gives milk, thier cow also gives milk";
const matches = s.matchAll(/cow (.*?) milk/g);
console.log(Array.from(matches, x => x[1]));
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    I have written a general [article about extracting strings between two strings with regex](https://www.buymeacoffee.com/wstribizew/extracting-text-two-strings-regular-expressions), too, feel free to read if you have a problem approaching your current similar problem. – Wiktor Stribiżew Feb 06 '21 at 22:03
  • FYI even latest safari breaks on `?<=` in your 2018 solution. – B-Stewart Jan 20 '23 at 05:22
58

Here's a regex which will grab what's between cow and milk (without leading/trailing space):

srctext = "My cow always gives milk.";
var re = /(.*cow\s+)(.*)(\s+milk.*)/;
var newtext = srctext.replace(re, "$2");

An example: http://jsfiddle.net/entropo/tkP74/

entropo
  • 2,481
  • 15
  • 15
20

The chosen answer didn't work for me...hmm...

Just add space after cow and/or before milk to trim spaces from " always gives "

/(?<=cow ).*(?= milk)/

enter image description here

duduwe
  • 888
  • 7
  • 16
  • 2
    Look Behind `?<=` is not supported in Javascript. – Mark Carpenter Jr Aug 17 '18 at 13:35
  • 2
    @MarkCarpenterJr if you tested it via https://www.regextester.com, you will get that hint. It seems that the site has based its rules from the older specification. Lookbehind is now supported. See https://stackoverflow.com/questions/30118815/why-are-lookbehind-assertions-not-supported-in-javascript/30119315 And the pattern works well with modern browsers without error. Try this checker instead https://regex101.com/ – duduwe Sep 08 '18 at 15:32
  • Does not work on iphone 14 safari – B-Stewart Jan 20 '23 at 05:25
20
  • You need capture the .*
  • You can (but don't have to) make the .* nongreedy
  • There's really no need for the lookahead.

    > /cow(.*?)milk/i.exec('My cow always gives milk');
    ["cow always gives milk", " always gives "]
    
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 1
    In this particular instance, if it were greedy it would reach the end and backtrack (presumably). – Ben Apr 12 '11 at 22:24
16

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"
Chase Oliphant
  • 379
  • 2
  • 5
  • 2
    Works for me! fantastic answer because it's just really simple! :) – Andrew Irwin Feb 28 '20 at 10:26
  • 4
    It misses two edge cases. 1. If start is missing from main string then it will throw exception. 2. If end is missing from main string then it will still gives the result back which would be wrong match. – Shailesh Sep 26 '20 at 06:47
13

You can use the method match() to extract a substring between two strings. Try the following code:

var str = "My cow always gives milk";
var subStr = str.match("cow(.*)milk");
console.log(subStr[1]);

Output:

always gives

See a complete example here : How to find sub-string between two strings.

thomas
  • 785
  • 8
  • 7
9

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
Radiodef
  • 37,180
  • 14
  • 90
  • 125
phil
  • 3,538
  • 2
  • 24
  • 24
5

Just use the following regular expression:

(?<=My cow\s).*?(?=\smilk)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brandon
  • 61
  • 1
  • 2
3

If the data is on multiple lines then you may have to use the following,

/My cow ([\s\S]*)milk/gm

My cow always gives 
milk

Regex 101 example

Naresh Kumar
  • 185
  • 3
  • 12
1

You can use destructuring to only focus on the part of your interest.

So you can do:

let str = "My cow always gives milk";

let [, result] = str.match(/\bcow\s+(.*?)\s+milk\b/) || [];

console.log(result);

In this way you ignore the first part (the complete match) and only get the capture group's match. The addition of || [] may be interesting if you are not sure there will be a match at all. In that case match would return null which cannot be destructured, and so we return [] instead in that case, and then result will be null.

The additional \b ensures the surrounding words "cow" and "milk" are really separate words (e.g. not "milky"). Also \s+ is needed to avoid that the match includes some outer spacing.

trincot
  • 317,000
  • 35
  • 244
  • 286
0

The method match() searches a string for a match and returns an Array object.

// Original string
var str = "My cow always gives milk";

// Using index [0] would return<br/>
// "**cow always gives milk**"
str.match(/cow(.*)milk/)**[0]**


// Using index **[1]** would return
// "**always gives**"
str.match(/cow(.*)milk/)[1]
TylerH
  • 20,799
  • 66
  • 75
  • 101
0

Task

Extract substring between two string (excluding this two strings)

Solution

let allText = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum";
let textBefore = "five centuries,";
let textAfter = "electronic typesetting";
var regExp = new RegExp(`(?<=${textBefore}\\s)(.+?)(?=\\s+${textAfter})`, "g");
var results = regExp.exec(allText);
if (results && results.length > 1) {
    console.log(results[0]);
}
Vasily Bodnarchuk
  • 24,482
  • 9
  • 132
  • 127