Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to delete brackets after a special letter in regex

Hi I am having problem while trying to remove these square brackets.

I figured out how to find square brackets but I need to find square brackets only if it starts with @ like this,

by the way I am using .replace to remove them in javascript, Not sure if it is going to help to find the answer.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@[john_doe]

The result must be @john_doe.

I dont want to remove other brackets which is like that,

[something written here]

Here is the link of the regex

>Solution :

You need a regular expression replace solution like

text = text.replace(/@\[([^\][]*)]/g, "@$1")

See the regex demo.

Pattern details

  • @\[ – a @[ text
  • ([^\][]*) – Group 1 ($1): any zero or more chars other than [ and ] (the ] is special inside character classes (in ECMAScript regex standard, even at the start position) and need escaping)
  • ] – a ] char.

See the JavaScript demo:

let text = '@[john_doe] and [something written here]';
text = text.replace(/@\[([^\][]*)]/g, "@$1");
console.log(text);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading