This regex matches "so" when it's preceded by a comma and a space:
(,\s)(so)
I want to do the opposite now. I want to tell the regex: don't match "so" if it's preceded by a comma and a space. I tried this (after seeing this SO question):
^(,\s)(so)
But now the regex doesn't match anything: https://regexr.com/4kgq8.
Note: I'm not trying to match beginning of the line.
I want to tell the regex: don't match "so" if it's preceded by a comma and a space.
Best solution is to use a negative lookbehind as I mentioned in my comment below question:
/(?<!, )so/g
Here, (?<!, )
is a negative lookbehind expression that fails the match if a comma and space is present before so
.
Caveat is that lookbehind
support in Javascript is only available in modern browsers.
If you want to support legacy or older browsers also then approach will be to use a captured group and discard unwanted match in alternation:
/(?:, so|(\bso))\b/g
Here a match is defined by presence of capture group #1 in each match. We are matching and discarding unwanted match of ", so
in left hand side of alternation. Our desired matches string is on right hand side of alternation which is captured in group #1.
Code:
var arr = ['So that',
'so big that',
', so so'];
const regex = /(?:, so|(\bso))\b/g;
arr.forEach((el) => {
m = regex.exec(el);
if (m && m.length > 1)
console.log('Line; [', el, ' ] Start:', regex.lastIndex, m[1])
});
Or you can simply use alternation, if that'd be OK, with an expression without lookarounds such as:
, so|(\bso\b)
const regex = /, so|(\bso\b)/gmi;
const str = `So that
so big that
, so
and not , so
, so so`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
I tried to do this calculation with Node.js. let x = 841251657 console.log((x*x)-x) I got: 707704349563994000 printed. When I use julia I get 707704350405245649 for the same calculation. And I got ...
I tried to do this calculation with Node.js. let x = 841251657 console.log((x*x)-x) I got: 707704349563994000 printed. When I use julia I get 707704350405245649 for the same calculation. And I got ...
In many places I'm setting modified by, created by etc: const test = this.state.isValueEdited ? { modifiedById: getLoggedInUser().userId, } : { // TODO }; How could I check if getLoggedInUser has ...
In many places I'm setting modified by, created by etc: const test = this.state.isValueEdited ? { modifiedById: getLoggedInUser().userId, } : { // TODO }; How could I check if getLoggedInUser has ...
I have an Array with Objects, there some of them has same "Subarrays" but with different labels. I need to summarize those based on equality. Comparing two different arrays is easy so far, but I am ...
I have an Array with Objects, there some of them has same "Subarrays" but with different labels. I need to summarize those based on equality. Comparing two different arrays is easy so far, but I am ...
I have an array with multiple nested arrays. Each nested array has three objects. I am trying to delete the second one but at the moment, I am getting a null value in its place. All I want is the ...
I have an array with multiple nested arrays. Each nested array has three objects. I am trying to delete the second one but at the moment, I am getting a null value in its place. All I want is the ...