155

Here is a regex that works fine in most regex implementations:

(?<!filename)\.js$

This matches .js for a string which ends with .js except for filename.js

Javascript doesn't have regex lookbehind. Is anyone able put together an alternative regex which achieve the same result and works in javascript?

Here are some thoughts, but needs helper functions. I was hoping to achieve it just with a regex: http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript

James
  • 77,877
  • 18
  • 158
  • 228
daniel
  • 1,683
  • 2
  • 11
  • 9
  • 3
    if you just need to check a specific filename or list of filenames, why not just use two checks? check if it ends in .js and then if it does, check that it doesn't match filename.js or vice versa. – si28719e Sep 11 '11 at 04:10
  • 3
    Update: The latest public Chrome version (v62) includes (presumably experimental) lookbehinds out of the box :D Note however that lookbehinds are still in proposal stage 3: https://github.com/tc39/proposal-regexp-lookbehind . So, it may take a while until JavaScript everywhere supports it. Better be careful about using in production! – Eirik Birkeland Nov 07 '17 at 14:08
  • 3
    Just use **`(?<=thingy)thingy`** for _positive lookbehind_ and **`(? – Константин Ван Feb 08 '18 at 16:51
  • 7
    @K._ As of Feb 2018 **that's not true** yet!! And it will need some time because browsers and engines must implement the specification (current in draft). – Andre Figueiredo Feb 22 '18 at 14:23
  • 2
    @AndreFigueiredo Yes, you're right. The **proposal** is currently on _Stage 4_. Maybe I was thinking of only _Chrome_, I guess. – Константин Ван Mar 07 '18 at 12:05
  • 3
    # Update: ES2018 includes [lookbehind assertions](https://github.com/tc39/proposal-regexp-lookbehind) [Plus](https://mathiasbynens.be/notes/es-regexp-proposals): - dotAll mode (the s flag) - Lookbehind assertions - Named capture groups - Unicode property escapes – Ashley Coolman Jan 26 '18 at 11:29
  • nodejs http://kangax.github.io/compat-table/es2016plus/ supports it – Muhammad Umer May 17 '19 at 02:55
  • 1
    Firefox still hasn't implemented the 2018 specification which prescribes support for look-behinds. Here's the [bug](https://bugzilla.mozilla.org/show_bug.cgi?id=1225665). – Lonnie Best Dec 08 '19 at 00:27
  • 1
    @LonnieBest meanwhile fixed for FF ([5 days ago](https://bugzilla.mozilla.org/show_bug.cgi?id=1225665#c35)) :-) – Wolf May 19 '20 at 13:58
  • @Wolf : That's fantastic news. When will it land? Version 77? – Lonnie Best May 19 '20 at 14:42
  • looks [like 78](https://bugzilla.mozilla.org/show_bug.cgi?id=1634135) (see ` Milestone: mozilla78`) – Wolf May 19 '20 at 16:15

6 Answers6

165

EDIT: From ECMAScript 2018 onwards, lookbehind assertions (even unbounded) are supported natively.

In previous versions, you can do this:

^(?:(?!filename\.js$).)*\.js$

This does explicitly what the lookbehind expression is doing implicitly: check each character of the string if the lookbehind expression plus the regex after it will not match, and only then allow that character to match.

^                 # Start of string
(?:               # Try to match the following:
 (?!              # First assert that we can't match the following:
  filename\.js    # filename.js 
  $               # and end-of-string
 )                # End of negative lookahead
 .                # Match any character
)*                # Repeat as needed
\.js              # Match .js
$                 # End of string

Another edit:

It pains me to say (especially since this answer has been upvoted so much) that there is a far easier way to accomplish this goal. There is no need to check the lookahead at every character:

^(?!.*filename\.js$).*\.js$

works just as well:

^                 # Start of string
(?!               # Assert that we can't match the following:
 .*               # any string, 
  filename\.js    # followed by filename.js
  $               # and end-of-string
)                 # End of negative lookahead
.*                # Match any string
\.js              # Match .js
$                 # End of string
Tim Pietzcker
  • 313,408
  • 56
  • 485
  • 544
  • Works on lots of cases except where there are preceeding characters, for example: filename.js (works-nomatch) filename2.js (works-match) blah.js (works - match) 2filename.js (doesn't work - nomatch) --- having said that, the lookbehind has the same limitation which I didn't realise until now... – daniel Sep 11 '11 at 07:40
  • 9
    @daniel: Well, your regex (with lookbehind) also doesn't match `2filename.js`. My regex matches in exactly the same cases as your example regex. – Tim Pietzcker Sep 11 '11 at 17:51
  • Forgive my naivety but is there a use for the non capturing group here? I've always known that to be only useful when trying to glean back reference for replacement in a string. As far as I know, this too will work ^(?!filename\.js$).*\.js$ – I Want Answers Mar 28 '17 at 06:46
  • 1
    Not quite, that regex checks for "filename.js" only at the start of the string. But `^(?!.*filename\.js$).*\.js$` would work. Trying to think of situations where the ncgroup might still be necessary... – Tim Pietzcker Mar 28 '17 at 06:53
  • This approach can be summarized as: instead of looking behind X, look ahead at every character that comes before X? – Sarsaparilla Jun 29 '18 at 07:27
  • @HaiPhan: Yes, but re-reading my answer I just noticed that there is a vastly less complicated solution that I had completely overlooked. Will update my answer X-) – Tim Pietzcker Jun 29 '18 at 08:26
  • [Firefox](https://bugzilla.mozilla.org/show_bug.cgi?id=1225665) still doesn't support look-behinds as prescribed by the 2018 specification. I understand they're actively working on it though. – Lonnie Best Jan 17 '20 at 07:08
  • BTW, I really like how you break the RegEx apart and describe it so concisely. – Lonnie Best Jan 17 '20 at 07:10
  • Really appreciate the breakdown of what each component does. Thanks! – Kamal Jun 07 '21 at 17:46
67

^(?!filename).+\.js works for me

tested against:

  • test.js match
  • blabla.js match
  • filename.js no match

A proper explanation for this regex can be found at Regular expression to match string not containing a word?

Look ahead is available since version 1.5 of javascript and is supported by all major browsers

Updated to match filename2.js and 2filename.js but not filename.js

(^(?!filename\.js$).).+\.js

Community
  • 1
  • 1
Benjamin Udink ten Cate
  • 12,717
  • 4
  • 45
  • 67
  • 8
    That question you linked to talks about a slightly different problem: matching a string that doesn't contain the target word *anywhere*. This one is much simpler: matching a string that doesn't *start with* the target word. – Alan Moore Sep 11 '11 at 05:50
  • Thats really nice, it only misses out on cases like: filename2.js or filenameddk.js or similar. This is a no match, but should be a match. – daniel Sep 11 '11 at 07:18
  • 10
    @daniel You asked for a look-behind, not a look-ahead, why did you accepted this answer? – hek2mgl May 28 '15 at 08:56
  • I'm grave-digging here, but the updated one has a broken/useless capture group and matches "filename.js" in the string "filename.json". It should be `^(?!filename\.js$).+\.js$` – Domino Jul 16 '15 at 13:30
  • 1
    the given one does not match on `a.js` – inetphantom Mar 17 '16 at 12:35
  • 1
    The original regex with lookbehind doesn't match `2filename.js`, but the regex given here does. A more appropriate one would be `^(?!.*filename\.js$).*\.js$`. This means, match any `*.js` *except* `*filename.js`. – weibeld May 16 '17 at 05:16
26

Let's suppose you want to find all int not preceded by unsigned:

With support for negative look-behind:

(?<!unsigned )int

Without support for negative look-behind:

((?!unsigned ).{9}|^.{0,8})int

Basically idea is to grab n preceding characters and exclude match with negative look-ahead, but also match the cases where there's no preceeding n characters. (where n is length of look-behind).

So the regex in question:

(?<!filename)\.js$

would translate to:

((?!filename).{8}|^.{0,7})\.js$

You might need to play with capturing groups to find exact spot of the string that interests you or you want't to replace specific part with something else.

Kamil Szot
  • 16,471
  • 6
  • 54
  • 64
3

If you can look ahead but back, you could reverse the string first and then do a lookahead. Some more work will need to be done, of course.

Albert Friend
  • 65
  • 1
  • 1
3

This is an equivalent solution to Tim Pietzcker's answer (see also comments of same answer):

^(?!.*filename\.js$).*\.js$

It means, match *.js except *filename.js.

To get to this solution, you can check which patterns the negative lookbehind excludes, and then exclude exactly these patterns with a negative lookahead.

Community
  • 1
  • 1
weibeld
  • 11,100
  • 2
  • 27
  • 46
-1

Below is a positive lookbehind JavaScript alternative showing how to capture the last name of people with 'Michael' as their first name.

1) Given this text:

const exampleText = "Michael, how are you? - Cool, how is John Williamns and Michael Jordan? I don't know but Michael Johnson is fine. Michael do you still score points with LeBron James, Michael Green Miller and Michael Wood?";

get an array of last names of people named Michael. The result should be: ["Jordan","Johnson","Green","Wood"]

2) Solution:

function getMichaelLastName2(text) {
  return text
    .match(/(?:Michael )([A-Z][a-z]+)/g)
    .map(person => person.slice(person.indexOf(' ')+1));
}

// or even
    .map(person => person.slice(8)); // since we know the length of "Michael "

3) Check solution

console.log(JSON.stringify(    getMichaelLastName(exampleText)    ));
// ["Jordan","Johnson","Green","Wood"]

Demo here: http://codepen.io/PiotrBerebecki/pen/GjwRoo

You can also try it out by running the snippet below.

const inputText = "Michael, how are you? - Cool, how is John Williamns and Michael Jordan? I don't know but Michael Johnson is fine. Michael do you still score points with LeBron James, Michael Green Miller and Michael Wood?";



function getMichaelLastName(text) {
  return text
    .match(/(?:Michael )([A-Z][a-z]+)/g)
    .map(person => person.slice(8));
}

console.log(JSON.stringify(    getMichaelLastName(inputText)    ));
Piotr Berebecki
  • 7,198
  • 4
  • 30
  • 42