javascript regex - look behind alternative?
^(?!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
Javascript Regex negative lookbehind Alternative
You may use
/^(?!.*\.h\.scss$).*\.scss$/
See the regex demo
Details
^
- start of string anchor(?!.*\.h\.scss$)
- a negative lookahead failing the match if the string ends with.h.scss
.*
- any 0+ chars as many as possible\.scss
- a.scss
substring at the...$
- end of the string.
Regex lookbehind alternative for Safari
I doubt there is a way with a single regex with no lookbehind or conditionals support thus I suggest the following approach combining regex and additional programming logic:
var texts = ['abcfobar','abc33bar'];
var regex = /^([a-zA-Z0-9]{0,3})(?:([a-zA-Z]{1,2})([a-zA-Z0-9]+)?)?/;
for (var i=0; i<texts.length;i++) {
var serialCode = '';
var serialMarket = '';
var serialSuffixmatch = '';
var match = texts[i].match(regex);
if (match) {
serialCode = match[1];
serialMarket = (match[2] || "");
if (serialCode.length == 3 && serialMarket.length == 2) {
serialSuffixmatch = match[3];
}
}
console.log(texts[i], '=>', serialCode + "\n" + serialMarket + "\n" + serialSuffixmatch);
}
Safari RegEx lookbehind alternative
Use
let regex = new RegExp(
`(<mark class="keyword_\\d">)?(?!\\B\\w)${this.escapeRegExp(keyword.value)}(?!</mark>)(?!\\w)`,
'guim')
highlightedContent = highlightedContent.replace(regex, (match, gr1) =>
gr1 ? match : `<mark class="keyword_${keyword.index}">${match}</mark>`)
Alternative to positive lookbehind regex
If you can only provide a regex and the whole match value is highlighted, there is no way to mimic the current lookbehind pattern. The best you could think of is a regex like/\b\s(\w+)(?=\s\w)/
that would also highlight the leading whitespace (unless there is an option to highlight only some group value).
If you can tweak the inner code, you could use a group replacement: find /(\w\s)(\w+)(?=\s\w)/
and replace with $1<span class="highlight">$2</span>
.
Is there an alternative for lookbehind in this case?
You can use
\b((a[a-c])(?:[^\"]*[^\"0-9])?2(?![0-9])[^\"]*)
The (?:[^\"]*[^\"0-9])?
part is a non-capturing group matching one or zero occurrences of any zero or more chars other than "
and then any one char that is not a "
and a digit.
Note that the consuming character of the added group is fine here since the [^\"]*
pattern was able to match an empty string, hence the use of the optional non-capturing group.
Alternative to negative lookbehind?
Let's first consider how it would be done with a lookbehind.
Then we just check if before what we capture is the start of the line, or a whitespace:
(?<=^|\s)(\.\d{5,})
We could simply change that lookbehind to a normal capture group.
Which means a preceding whitespace also gets captured. But in a replace we can just use or not use that capture group 1.
(^|\s)(\.\d{5,})
In the PCRE regex engine we have \K
\K : resets the starting point of the reported match. Any previously
consumed characters are no longer included in the final match
So by using that \K in the regex, the preceding space isn't included in the match
(?:^|\s)\K(\.\d{5,})
A test here
However, if you use Rubi's scan
with a regex that has capture groups?
Then it seems that it only outputs the capture groups (...)
, but not the non-capture groups (?:...)
or what's not in a capture group.
For example:
m = '.12345 .123456 NOT.1234567'.scan(/(?:^|\s)(\.\d{5,})/)
=> [[".12345"], [".123456"]]
m = 'ab123cd'.scan(/[a-z]+(\d+)(?:[a-z]+)/)
=> [["123"]]
So when you use scan, lookarounds don't need to be used.
JavaScript regex: Positive lookbehind alternative (for Safari and other browsers that do not support lookbehinds)
Turn the lookbehind in a consuming pattern and use a capturing group:
And use it as shown below:
var s = "some string.005";
var rx = /\.\d\d(\d)/;
var m = s.match(/\.\d\d(\d)/);
if (m) {
console.log(m[1]);
}
Regex positive lookbehind alternative for word wrap
Use
return text.replace(/(.{45})(?!$)/g, "$1\r\n");
See regex proof.
EXPLANATION
NODE EXPLANATION
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
.{45} any character except \n (45 times)
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of look-ahead
Alternatives to positive lookbehind REGEX when using JDE
Match but exclude from the result action is performed by lookbehinds or \K
and by capturing groups.
Capture the part you need with the expression, and set the result to Group 1.
If this is not supported by your tool, log an enhancement request.
If regex replacement is allowed, match the whole string and capture the part you need, replace with a backreference (\1
or $1
), see proof.
Related Topics
What Values Can a Constructor Return to Avoid Returning This
How to Format Time Since Xxx E.G. "4 Minutes Ago" Similar to Stack Exchange Sites
Truncate Number to Two Decimal Places Without Rounding
JavaScript Regex Multiline Text Between Two Tags
How to Use Jquery for Xml Parsing with Namespaces
Why Variable Hoisting After Return Works on Some Browsers, and Some Not
Code Within D3.JSON() Callback Is Not Executed
How to Check If Object Property Exists with a Variable Holding the Property Name
Object Spread VS. Object.Assign
Javascript: Filter() for Objects
Why Does the Month Argument Range from 0 to 11 in JavaScript's Date Constructor
Get String in Yyyymmdd Format from Js Date Object
Advantages of Createelement Over Innerhtml
Defining Methods via Prototype VS Using This in the Constructor - Really a Performance Difference