regexp and curly brackets

A

Andrew Poulos

I been given a string where I need to replace specific words surrounded
by curly brackets. For example

var str = "Hello {UN}";

and I replace {UN} with the user's name using:

var username = "Andrew";

var re = /{UN}/gi;
var newstr = str.replace(re, usersname);

Its only now that I realised that curly brackets have in fact a specific
purpose in regexp.

My question is, does it matter in this case as the content between the
curly brackets is not valid as far as the special meaning for curly
brackets in regexp goes, or does the regexp need to change to:

var re = /\{UN\}/gi;

Or is there a better way?

I did some testing and it seemed to always do the replacement.

Andrew Poulos
 
T

Trevor Lawrence

I had a boss at work once whose favourite saying (or at least the one that I
remember most) was
"If it works, use it"

Of course the purists at this site may say otherwise.

My opinion which may not be worth as much as that of the experts here is to
use the escape character
 
B

Bart Van der Donck

Andrew said:
I been given a string where I need to replace specific words surrounded
by curly brackets. For example

var str = "Hello {UN}";

and I replace {UN} with the user's name using:

var username = "Andrew";

var re = /{UN}/gi;
var newstr = str.replace(re, usersname);

Its only now that I realised that curly brackets have in fact a specific
purpose in regexp.

My question is, does it matter in this case as the content between the
curly brackets is not valid as far as the special meaning for curly
brackets in regexp goes, or does the regexp need to change to:

var re = /\{UN\}/gi;

Alternatively, you might convert the character to its Unicode point
(\u....), before executing the regular expression.

--
Bart

"It is absolutely necessary for technical reasons that these nuclear
warheads are stored with the TOP at the BOTTOM and the BOTTOM at the
TOP. In order that there may be no doubt as to which is the TOP and
which is the BOTTOM, for storage purposes, it shall be seen that the
BOTTOM of each nuclear warhead is labelled with the word TOP."
(Instructions issued by the British Admiralty, 1950's)
 
T

Thomas 'PointedEars' Lahn

Trevor said:
I had a boss at work once whose favourite saying (or at least the one
that I remember most) was "If it works, use it"

An approach that produces inflexible code, ultimately harmful in such a
dynamic environment like the Web.
Of course the purists at this site may say otherwise.

Which "site" are you talking about? Could it be that you have no clue where
you are posting to?

| From: "Trevor Lawrence" <Trevor L.@Canberra>
^ ^^^^^^^^
I see. Question retracted.
My opinion which may not be worth as much as that of the experts here is
to use the escape character

This opinion of yours is worthless, indeed.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Bart said:
Alternatively, you might convert the character to its Unicode point
(\u....), before executing the regular expression.

That's an incompatible approach, not to be recommended.

Your signature is much too long and delimited wrong.


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <5853e0e0-1df9-454a-8fb8-739892715274@k3
6g2000yqe.googlegroups.com>, Mon, 1 Dec 2008 04:35:47, Bart Van der
Donck said:
Alternatively, you might convert the character to its Unicode point
(\u....), before executing the regular expression.

Or \x60, \x62, maybe,
"It is absolutely necessary for technical reasons that these nuclear
warheads are stored with the TOP at the BOTTOM and the BOTTOM at the
TOP. In order that there may be no doubt as to which is the TOP and
which is the BOTTOM, for storage purposes, it shall be seen that the
BOTTOM of each nuclear warhead is labelled with the word TOP."
(Instructions issued by the British Admiralty, 1950's)

Wrong approach. They should in fact be designed the other way up,
stored the right way up, and dropped upside-down.
 
T

Trevor Lawrence

Conrad Lender said:
By escaping the curly brackets, the become just another character, as
far as the regex is concerned. /\{UN\}/ is the correct way to match
strings with special characters.

Be careful with giving advice.

I replied that in my opinion this would be the thing to do (i.e. escape the
curly brackets) but I was informed that my opinion was worthless. Can
someone can tell me why this is so ?
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
I replied that in my opinion this would be the thing to do (i.e. escape the
curly brackets) but I was informed that my opinion was worthless. Can
someone can tell me why this is so ?

It is because Thomas Lahn is an ill-tempered control freak. Google the
group for articles by him, and the distasteful experience will confirm
that.

But, if you read the FAQ and the references in my sig, you can give him
less incitement to vent his spleen. It will not stop him; but it will
make your articles more pleasing to the normal regulars.
 
L

Lasse Reichstein Nielsen

Andrew Poulos said:
var re = /{UN}/gi;
var newstr = str.replace(re, usersname);

Its only now that I realised that curly brackets have in fact a
specific purpose in regexp.


To murky the waters even more, many RegExp implementations are so
terrified of giving a syntax error, that they will read /{UN}/ as
/\{UN\}/.

That's no excuse for using it though. Two wrongs don't make a right.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top