javascript regex - look behind alternative?
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 javascrip开发者_Go百科t?
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
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
^(?!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
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.
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.
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.
Thanks for the answers from Tim Pietzcker and other persons. I was so inspired by their works. However, there is no any ideal solution, I think, for mimicking lookbehind. For example, solution from Pietzcker is limited by $
as EOL, that is, without $
there would get unexpected result:
let str="filename.js main.js 2022.07.01"
console.log( /^(?!.*filename\.js).*\.js/g.exec(str) ) //null
Another limitation is that it is hard to translate multiply lookbehinds, such as:
let reg=/(?<!exP0)exp0 \d (?<!exP1)exp1 \d (?<!exP2)exp2/
How to build a more generic and free method to use lookbehind assertion alternatively? Bellow is my solution.
The core pattern of alternative code is:
(?:(?!ExpB)....|^.{0,3})ExpA <= (?<!ExpB)ExpA
Detail explanation:
(?: # start an unsave group:
(?!ExpB) # Assert a possion who can't match the ExpB
.... # Any string, the same length as ExpB
|^.{0,3} # Or match any string whoes length is less than ExpB
) # End of negative lookahead
ExpA # Match ExpA
For instance:
var str="file.js main.js 2022.07.01"
var reg=/(?:(?!file)....|^.{0,3})\.js/g // <= (?<!file)\.js
console.log( reg.exec(str)[0] ) // main.js
Here is an implement to translate above pattern into a sugar:
var str="file.js main.js 2022.07.01"
var reg=newReg("﹤4?!file﹥\\.js","g") //pattern sugar
console.log(reg.exec(str)[0]) // main.js
function newReg(sReg,flags){
flags=flags||""
sReg=sReg.replace(/(^|[^\\])\\﹤/g,"$1<_sl_>").replace(/(^|[^\\])\\﹥/g,"$1<_sr_>")
if (/﹤\?<?([=!])(.+?)﹥/.test(sReg)){
throw "invalid format of string for lookbehind regExp"
}
var reg=/﹤(\d+)\?<?([=!])(.+?)﹥/g
if (sReg.match(reg)){
sReg=sReg.replace(reg, function(p0,p1,p2,p3){
return "(?:(?"+p2+p3+")"+".".repeat(parseInt(p1))+"|^.{0,"+(parseInt(p1)-1)+"})"
})
}
sReg=sReg.replace(/<_sl_>/g,"﹤").replace(/<_sr_>/g,"﹥")
var rr=new RegExp(sReg,flags)
return rr
}
Two special characters ﹤
( \uFE64 or ﹤
) and ﹥
( \uFE65 or ﹥
) are used to enclose the lookbehind expression, and a number N
counting the length of lookbehind expression must follow the ﹤
. That is ,the syntax of lookbehind is:
﹤N?!ExpB﹥ExpA <= (?<!ExpB)ExpA
﹤N?=ExpB﹥ExpA <= (?<=ExpB)ExpA
To make the pattern above more ES5-like, you can replace ﹤
or ﹥
with parenthesis and remove N
, by writing more code into newReg()
function.
I know this answer is not tackling really how to rewrite a regex to simulate lookbehinds, but i managed to overcome some very simple situations like this one by replacing the unwanted match from the string beforehand, as in:
let string = originalString.replace("filename.js", "filename_js")
string.match(/.*\.js/)
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) ));
精彩评论