If you just want to get rid of all square brackets you could do this:

stringValue.replace(/[\[\]]/g, "");

This is a little confusing since I'm using a square bracket enclosed
"character class", as well as having to escape the literal square brackets.
 It's a little more simple if you imagine you wanted to do the same thing
with two arbitrary characters, for example A and Z, which would look like
this:

stringValue.replace(/[AZ]/g, ""); // converts "Aone two threeZ" into "one
two three"

Character classes will match any character from the list inside them and
they take ranges, so [AZ] is "A or Z" whereas [A-Z] is "A or B or C or ...
Z".  You can negate a character class by using a caret as the first
character (so [^AZ] means "anything other than an A or a Z) but don't get
confused with ^ outside of a character class which means "the beginning of
the string".  We have to add the "g" flag after the regexp because we want
to replace all of the square brackets, otherwise it would only match the
first character it found that was in the class list.  Without the /g at the
end, it would be this:

stringValue.replace(/[AZ]/, ""); // converts "Aone two threeZ" into "one two
threeZ"

In order to literally refer to a square bracket, you have to escape them by
preceding them with a backslash, so \[ means "match a square bracket" rather
than "start a character class", for example:

stringValue.replace(/\[AZ\]/g, "az"); // converts "foo [AZ] bar" into "foo
az bar"

So, we can actually construct a character class which is "open square
bracket OR close square bracket", and that looks like this:

stringValue.replace(/[\[\]]/g, ""); // converts "[something goes here]" into
"something goes here"

Note that you could just as well swap the order of the square brackets
inside the class:

stringValue.replace(/[*\]\[*]/g, ""); // converts "[something goes here]"
into "something goes here"

This will strip out ALL square brackets, by the way.  If you only want to
strip out the first and last of each, its a bit more tricky.  Because I'm
not very good with regular expressions, I'd probably use two:

// converts "bracket one [ and two [ and three [ ..."
// into "bracket one  and two [ and three [ ..."
openStripped = stringValue.replace(/^([^\[]*)\[/, "$1");

// converts "... near ] the ] end ] and a bit more"
// into "... near ] the ] end and a bit more"
closeStripped = openStripped.replace(/\]([^\]]*)$/, "$1");

http://jsbin.com/axomev/2/edit


Pete



On 29 July 2011 14:09, Matthew Bramer <[email protected]> wrote:

> Thanks for the quick suggestions.  Let me see if I am digesting the info
> correctly:
>
> This part will replace the "[" at the beginning of the string:
>  stringValue.replace(/^\[
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> This part will find all "]", "SingleQuotes", "DoubleQuotes" and replace
> them with an empty string:
> \]'"/g, "")
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> What does this do in the context of the regex?
> |[
>
> stringValue.replace(/^\[|[\]'"/g, "")
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Cheers,
> Matt
>
>
>  --
> To view archived discussions from the original JSMentors Mailman list:
> http://www.mail-archive.com/[email protected]/
>
> To search via a non-Google archive, visit here:
> http://www.mail-archive.com/[email protected]/
>
> To unsubscribe from this group, send email to
> [email protected]
>



-- 
Pete Otaqui
[email protected]
+44 7949 945542

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to