2

I want to exclude characters from displaying in a vbulletin template.

For example, if a user writes:

"[Hello World] How are you?"

I want to ecxlude "[" and "]" all that's inside so it only displays:

"How are you?"

Is there a way to do this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sev
  • 71
  • 7
  • Look at this [answer](https://stackoverflow.com/questions/2403122/regular-expression-to-extract-text-between-square-brackets) and [`String.prototype.replace()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) method – AlexSkorik Dec 26 '18 at 15:02

1 Answers1

0

Use JavaScript string operations .getIndexOf() and .substring(). Get the position of the first bracket, get the position of the second bracket, split the string into 3 substrings, the middle section being between the two indexed values, and then add just the first and third substrings together. Like this:

var string = "[Hello World] How are you?";
var bracket1 = string.getIndexOf("[");
var bracket2 = string.getIndexOf("]");
var substring1 = string.substring(0,bracket1);
var substring2 = string.substring(bracket1,bracket2);
var substring3 = string.substring(bracket2,string.length);
var solution = substring 1 + " " + substring 3;

At least, that's the concept. Everything may not be right on, but you could futz with the numbers a little to get it perfect.

Or if you don't need to worry about what comes before [], simply use .split():

 var string = "[Hello World] How are you?";
 var solutionArray = string.split("]");
 var solution = solutionArray[1];

Hope this helps!

zdebruine
  • 3,687
  • 6
  • 31
  • 50