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]);
}
javascript regex - look behind alternative?
^(?!filename).+\.js
works for me
tested against:
- test.js match
- blabla.js match
- filename.js no match
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
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
JS Regex lookbehind not working in firefox and safari
Firefox and Has
Safari doesn't have support for lookbehind yet, you can use capture group ( used so that this the pattern on which we are splitting will be also be added in output ) and split on <tag> </tag>
let str = "The quick brown <tag>fox</tag> jumps over the lazy <tag>dog</tag>"
let regex = /(<tag>.*?<\/tag>)/
console.log(str.split(regex).filter(Boolean))
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);
}
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]);
}
JavaScript support of Lookaheads and Lookbehinds in Regular Expressions
Javascript has support for only positive and negative lookahead with no support whatsoever for lookbehinds, but you can still mimic the latter in Javascript using callbacks.
There is a nice article about this here, actually although this article uses callbacks to provide some sort of an alternative support for lookbehind, the same principle can be used in other languages that support lookbehinds but not variable expressions in them so it is handy trick.
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>
.
Trying to find regex lookbehind alternative for GAS
v[r][0] = v[r][0].replace(/^(.{5})(:00)/, "$1");
This breaks the regular expression into two capture groups, and the "$1"
just returns the first group.
Related Topics
How to Access Object Using Dynamic Key
How to Sign into Google with Selenium Automation Because of "This Browser or App May Not Be Secure."
JavaScript Property with Three Dots (...)
Select a Complete Table with JavaScript (To Be Copied to Clipboard)
Do We Need Semicolon at the End
How to Compare 2 Functions in JavaScript
JavaScript Settimeout and Loops
Convert Unix Timestamp to Date Time (Javascript)
D3.JSON Method Doesn't Return My Data Array
Adding Code to a JavaScript Function Programmatically
Getting Access Is Denied Error on IE8
Js/Es6: Destructuring of Undefined
How to Pass a Flag to Gulp to Have It Run Tasks in Different Ways
Angularjs - UI Router - Programmatically Add States