-1

I am using nodejs v8.9.4 and typescript v2.6.2. I want to build a Regexp that can turn on and off case sensitivity like it do the (?i) and (?-i) modifier spans for example in the .NET environment.
I want to accomplish something like:

let value = 'HelloWorld';
let pattern = '(?i)\\b(' + value + ')\\:|' +
    '(?-i)\\b(' + value.toUpperCase() + '|' + value + ')\\b';
let myRegex = new RegExp(pattern, 'g');

myRegex.test('helloworld');  // returns false
myRegex.test('helloworld:'); // returns true
myRegex.test('HELLOWORLD');  // returns true
myRegex.test('HelloWorld');  // returns true

Unfortunately javaScript does not support modifier spans.

So I am asking is there a way to build a Rexep in javaScript like the myRegex of the example above where the value is assignable/exchangeable.
Every hint is welcome, thank's in advance.

HaaLeo
  • 10,065
  • 3
  • 44
  • 55
  • Is that the actual way you’re going to use it? With that particular pattern? – Ry- Feb 09 '18 at 22:02
  • Yes I want to use that particular pattern. – HaaLeo Feb 09 '18 at 22:05
  • 1
    Can you make two `RegExp`s, then? One with the first part and `'gi'`, one with the second part and `'g'`, `a.test(input) || b.test(input)`? – Ry- Feb 09 '18 at 22:17
  • In general this would work. In my case I will call that regexp pretty often (up to > 60 times per min) and evaluate its `index` to update a document whether it changed. So I think due to performance issues one single regex would be the preferable approach. – HaaLeo Feb 09 '18 at 22:26
  • Even `XRegExp` can't handle this. So no one-liner regex solution. – revo Feb 09 '18 at 22:29
  • "up to > 60 times per min" is not "often". Several thousand times per second is often. And you forgot to regex-escape your `value`, that's a bug. Anyway, what is your regex supposed to match, in plain English? – Tomalak Feb 09 '18 at 22:47
  • What is the overall purpose of this? I'm looking at your pattern and can't understand why you wouldn't just use the case-insensitive form for everything. – CAustin Feb 09 '18 at 22:57
  • Sorry, I do not see where I forgot to escape the `value`. I write a feature for an extension for vscode where the user can set the `value` via the config. My extension detects a document change by hooking the `vscode.workspace.onDidChangeTextDocument` event. When the event fires I search the document with the regex and then colorize all matches by applying a decoration to the corresponding selection. The extension shall support multiple `value`s so there can be multiple regex calls per document change. The event fires on each keystroke. – HaaLeo Feb 09 '18 at 23:01
  • You must regex-escape the value before you build a regex from string concatenation. Think about what happens if the value contains `$` or `(` or any of the other characters that have meaning in regex. – Tomalak Feb 09 '18 at 23:09
  • @CAustin I want to highlight custom log level that were set by the user. And I want avoid highlighting occurrences in the log message. For example the user sets the log level to 'Verbose'. Then I want avoid highlighting it in the message "This is *verbose*.". Multiple log file layouts shall be supported. – HaaLeo Feb 09 '18 at 23:17
  • @Tomalak now I get it. Thanks for the hint. In the extension code I check for `if (/\w+/g.test(value))` before building that new regexp. – HaaLeo Feb 09 '18 at 23:21
  • It's much smarter to use one of the `RegExp.escape()` implementations available. Always, without ifs and buts, whenever you write code that builds regex from strings. There are even node packages that do this, or you simply pick up a one-liner, [like this one](https://stackoverflow.com/questions/3561493/). – Tomalak Feb 10 '18 at 07:52

1 Answers1

0

As suggested by Ryan in the comments I split the Regexp into two parts. So far I am not facing any performance issues or similar.
Thank's to the guys in the comments for helping out.

HaaLeo
  • 10,065
  • 3
  • 44
  • 55