Home » Javascript » Regexp: case different flags for different branches

Regexp: case different flags for different branches

Posted by: admin November 1, 2017 Leave a comment

Questions:

Using some regexp in Javascript. I’d like to be able to apply flags to different parts of the regexp. Here is what I have so far:

const expressionRegexPattern = new RegExp([
  /[\<\>\+\*\(\)\-\@\`]/,                                                       // math and other symbols except "="
  /|(^|[^!><'=])=($|[^=])/,                                                     // only single "=" are valid
  /|\band\b|\bor\b|\bnot\b/,                                                    // bitwise operators
  /|\btrue\b|\bfalse\b/,                                                        // bools
  /|\b[\d.]+\b/                                                                 // numbers
].map(function (r) {
  return r.source;
}).join(''), 'gi');

Basically, this get’s smashed down into the following regex:

'/[\<\>\+\*\(\)\-\@\`]|(^|[^!><'=])=($|[^=])|\band\b|\bor\b|\bnot\b|\btrue\b|\bfalse\b|\b[\d.]+\b/gi'

I then “cleanse” some string for any matching patterns of that regexp. The result is an array of values that DO NOT match any pattern.

const expression = 'TEST > 0.1 and something_else not true';
const cleansedArray = expression.replace(conditionExpressionRegexPattern, ' ').trim().split(/ +/);

Result would be an array of the following:

['TEST', 'something_else']

What I’d like to do is “add on” to this regex so that I can test other branches of a string, but make it be case sensitive. I can’t add them to the first regex since it has the case insensitive flag. Something like so:

const expressionRegexPattern = new RegExp([
  /[\<\>\+\*\(\)\-\@\`]/,                                                       // math and other symbols except "="
  /|(^|[^!><'=])=($|[^=])/,                                                     // only single "=" are valid
  /|\band\b|\bor\b|\bnot\b/,                                                    // bitwise operators
  /|\btrue\b|\bfalse\b/,                                                        // bools
  /|\b[\d.]+\b/                                                                 // numbers
].map(function (r) {
  return r.source;
}).join(''), 'gi') && new RegExp([/|\bmA\b|\bV\b|\bA\b/], 'g');

This doesn’t work, but hopefully gives you an idea of what I want to achieve. The words mA, V, and A would get matched on, but not MA, v, a. I’d like to be able to have some specific “words” be case sensitive matching, and bundle that up into my other regexp that is globally case insensitive.

Hopefully I explained this in a non-confusing way! Been trying to research this for quite some time now so figured I’d ask the geniuses of SO.

Answers:

As @Wiktor Stribiżew stated, in JS, regex flag work on whole regex, and not only part of it.

what you could do is to do a two step process. first get all you possible match and branch with case insensitive regex, and after filter out with case sensitive regex those who must be tested with case sensitiveness.