Quantcast

Forums | MacLife

You are not logged in.

#1 2009-01-23 11:09 pm

Jasoco
Your own personal Jesus
From: Doylestown, PA, USA, Earth
Registered: 2000-08-26
Posts: 8845
Website

Okay, I give. How do I replace a perenthesis in a string in JavaScript

I give up.

How do I replace parenthesis in a string in JavaScript?

In other words, if I have a string that contains a ( or a ), how do I use the JavaScript replace method to replace them with something else?

I tried the code you're supposed to use;

Code:

str.replace(/(/, "Replacement");

But it just fails.

So apparently I have to escape that character since I assume the ( and ) are reserved for something. So how the heck do I escape it and replace a ( or )?

confused


                         Haikus are easy
          But sometimes they don't make sense
                           Refrigerator

                Jasoco.netGeekPub Forums

Offline

 

#2 2009-01-24 2:10 am

Booksley
Zombie Genocidest
From: Toronto, Ontario
Registered: 2001-02-16
Posts: 5037

Re: Okay, I give. How do I replace a perenthesis in a string in JavaScript

Jasoco wrote:

I give up.

How do I replace parenthesis in a string in JavaScript?

In other words, if I have a string that contains a ( or a ), how do I use the JavaScript replace method to replace them with something else?

I tried the code you're supposed to use;

Code:

str.replace(/(/, "Replacement");

But it just fails.

So apparently I have to escape that character since I assume the ( and ) are reserved for something. So how the heck do I escape it and replace a ( or )?

confused

Looks like you're just missing the ' ' or " ".

Code:

str.replace('(', '[');

Offline

 

#3 2009-01-24 2:18 am

Jasoco
Your own personal Jesus
From: Doylestown, PA, USA, Earth
Registered: 2000-08-26
Posts: 8845
Website

Re: Okay, I give. How do I replace a perenthesis in a string in JavaScript

But that it were that simple.

Alas, that does not work. In fact, that's how I thought it was supposed to be, but no, instead of quotes in the first part, you need to use /'s. As every tutorial on the internets says. But none say how to escape a special reserved character like a ( or ). IT SHOULD NOT BE THIS DIFFICULT! sad

Using quotes however does not cause it to fail. But it still does not replace a thing.

Maybe I'm doing it wrong, maybe not. I can't get any replacements working. I'm in Bowtie FYI, but that shouldn't matter as a Bowtie theme is a Webkit. Same as a Dashboard Widget. Can you not replace in Dashboard either?


                         Haikus are easy
          But sometimes they don't make sense
                           Refrigerator

                Jasoco.netGeekPub Forums

Offline

 

#4 2009-01-24 1:01 pm

Basseq
Fixxy of F&S fame
From: D.C.
Registered: 2002-12-18
Posts: 3125
Website

Re: Okay, I give. How do I replace a perenthesis in a string in JavaScript

Code:

str.replace(/\(/g, '[');

That replaces based on a regular expression. The slashes (/) denote the expression. The letter 'g' after the regex indicates that the search should be global (otherwise it will just replace the first match). You have to escape regex characters (parentheses, brackets, pipes, periods, asterisk, etc.) by placing a backwards slash before them, the same way you would escape quotes in a string.

Code:

str.replace('(', '[');

This should also work. Remember, replace() does not modify the string. Be sure you're assigning the output to something, e.g.:

Code:

str = str.replace(/\(/g, '[');

Last edited by Basseq (2009-01-24 1:05 pm)


Basseq is me, John Whittet.
(Finishing the remainder of the thought expressed in the post has been left as an exercise for the reader.)

Offline

 

#5 2009-01-27 9:04 pm

Jasoco
Your own personal Jesus
From: Doylestown, PA, USA, Earth
Registered: 2000-08-26
Posts: 8845
Website

Re: Okay, I give. How do I replace a perenthesis in a string in JavaScript

The last one works.

Thanks a lot!


                         Haikus are easy
          But sometimes they don't make sense
                           Refrigerator

                Jasoco.netGeekPub Forums

Offline

 

#6 2009-01-27 10:24 pm

b_dubb
loch whatchamacallit
From: chapel hill, nc
Registered: 2002-11-19
Posts: 510
Website

Re: Okay, I give. How do I replace a perenthesis in a string in JavaScript

the Force is strong in Basseq


"The Fates lead he who will; he who won't, they drag." - Seneca

Offline

 

Board footer

Powered by PunBB 1.2.6
© Copyright 2002–2005 Rickard Andersson