Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove square brackets from string

I want to process some text to remove any substrings in square brackets, e.g. converting this:

We noted that you failed to attend your [appointment] last appointment . Our objective is to provide the best service we can for our patients but [we] need patients’ co-operation to do this. Wasting appointments impedes our ability to see all the patients who need consultations. [If] you need to cancel an appointment again we would be grateful if you could notify reception in good [time].

To this:

We noted that you failed to attend your last appointment . Our objective is to provide the best service we can for our patients but need patients’ co-operation to do this.Wasting appointments impedes our ability to see all the patients who need consultations.you need to cancel an appointment again we would be grateful if you could notify reception in good.

How can I achieve this?

like image 583
ankur Avatar asked Oct 03 '13 08:10

ankur


2 Answers

Use this regex....

var strWithoutSquareBracket = strWithSquareBracket.replace(/\[.*?\]/g,'');

The regex here first matches [ and then any character until a ] and replaces it with ''

Here is a demo

like image 182
iJade Avatar answered Sep 28 '22 03:09

iJade


Check this, it will help you:

var strWithoutSquareBracket = "Text [text]".replace(/\[.*?\]/g, '');

alert(strWithoutSquareBracket);

Fiddle Here

like image 30
S. S. Rawat Avatar answered Sep 28 '22 03:09

S. S. Rawat