0

Maybe I forgot how RegExp works but I tried to replace a text with empty values.

I have text like: this is a blabla (32332) [XAML] and I want to remove [] and ().

I want to do using RegExp object. In javascript, I'm doing like:

var obj = "this is a blabla (32332) [XAML]";
var patt1 = "[\(\)\]\[]";

var regObj = new RegExp(patt1, "gi");

obj = obj.replace(regObj, "");
console.log(obj);

The result is still same this is a blabla (32332) [XAML] means nothing is replaced.

Where is my mistake ? Here's fiddle: https://jsfiddle.net/weak5ub3/

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
Snake Eyes
  • 16,287
  • 34
  • 113
  • 221
  • You need to escape the first `]`. Also, better to [avoid new RegExp](https://stackoverflow.com/a/55793086/): instead use `/[()\][]/gi` – CertainPerformance Sep 12 '19 at 06:31
  • Try this ```console.log("this is a blabla (32332) [XAML]".replace(/[\(\)\]\[]/gi, ""))```. – Dipak Telangre Sep 12 '19 at 06:35
  • @CertainPerformance That regular expression I got as string from C# action MVC (from remote server)...It's single way to send C# regex to javascript regex to be executed on client side – Snake Eyes Sep 12 '19 at 06:38
  • Then you just need to escape the first `]` (and only that, nothing else needs escaping) https://regex101.com/r/6Mk5yS/1 – CertainPerformance Sep 12 '19 at 06:40
  • Your method of solving the problem is correct. The problem in your code however is that when you intended to escape the bracket characters for the regex here: `var patt1 = "[\(\)\]\[]";` they actually got escaped in the string `patt1` first, then fed to the regex as "[()][]", which ruins it. A solution is either to escape the escape character `\` in your string:`"[\\(\\)\\]\\[]"` or construct the regex directly through the syntax: `var regObj = /[\(\)\]\[]/gi;` – Rashad Saleh Sep 12 '19 at 06:47
  • This question has been marked as a duplicate by @CertainPerformance. However, I think that shouldn't be the case as: 1) the first suggested duplicate is about constructing the right regex, which is not the problem here (the regex works just fine). 2) the second suggested duplicate is asking why this problem exists, where the question here is asking what is the problem. – Rashad Saleh Sep 12 '19 at 06:49
  • @RashadSaleh This is essentially an escaping problem. The first link shows why OP's backslashes are getting lost, and how to fix it (double-escape them), and the second shows that `]` needs to be escaped in a character set. Seems pretty clear-cut to me. OP's regex does *not* work just fine (due to the escaping issues) - see the snippet in the question – CertainPerformance Sep 12 '19 at 06:52
  • @CertainPerformance I apologize, I have referenced "the first question" for the second, and "the second question" for the first in my previous comment. Regardless, I still stand by it. – Rashad Saleh Sep 12 '19 at 07:07

1 Answers1

0

You can use capture group and replace

enter image description here

var obj = "this is a blabla (32332) [XAML]";
var patt1 = /\(([^)]*)\)|\[([^\]]*)\]/gi;

obj = obj.replace(patt1, "$1$2");

console.log(obj)

If you want to remove [] and () irrespective of where they are, they are balanced or not, than you can simply use

  [\]()\[]+

Regex Demo


using RegExp object, you need is to escape special characters with \\ when you want to use regexp object

var obj = "this is a blabla (32332) [XAML]";
var patt1 = new RegExp("\\(([^)]*)\\)|\\[([^\\]]*)\\]", "gi");

obj = obj.replace(patt1, "$1$2");
console.log(obj)

var pat2 = new RegExp("[\\]()\\[]+", "gi")
var secondObj = obj.replace(pat2, "$1$2")
console.log(secondObj)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • I want RegExp object in `replace` method... – Snake Eyes Sep 12 '19 at 06:37
  • @SnakeEyes all you need is to escape with `//` when you want to use regexp object, but you should prefer regex literal unless you're trying to build a dynamic regex – Code Maniac Sep 12 '19 at 06:40
  • @CodeManiac Your long answer is unnecessary as the question does include the right regex. Your previous comment about escaping the `/` in the string used to build the regex should instead be the answer. – Rashad Saleh Sep 12 '19 at 06:52
  • @RashadSaleh thanks for your comment, but the first pattern isn't unnecessary, the question say `()` and `[]` so i tool it as it should match in balance not just any of them in any manner, so i wrote the first one, second one is just to handle the other case as op is doing, initially i had answer without regexp object as i feel thee no need of using `RegExp`, but since OP asked for that so i added as example there, yeah i agree escaping line can and should be added to answer itself – Code Maniac Sep 12 '19 at 06:57
  • The regex `[\(\)\]\[]` is both syntactically and semantically correct. The asker has shown sufficient understanding of regex's. – Rashad Saleh Sep 12 '19 at 07:16