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?
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?
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
Check this, it will help you:
var strWithoutSquareBracket = "Text [text]".replace(/\[.*?\]/g, '');
alert(strWithoutSquareBracket);
Fiddle Here
Let's say text
is the variable name that holds your text. From your example, it seems you want to remove all text between square brackets (along with said brackets), so you'll have to write this:
text = text.replace(/\[.+?\]/g, '');
The \[
matches the 1st bracket; then .+?
matches any characters until the closing bracket (matched by \]
). The g
in the end tells replace
to apply the regex to all the text.