Forums | MacLife
You are not logged in.
Pages: 1
- Index
- » Programming & Web Dev
- » Okay, I give. How do I replace a perenthesis in a string in JavaScript
#1 2009-01-23 11:09 pm
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 )?
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 )?
Looks like you're just missing the ' ' or " ".
Code:
str.replace('(', '[');Offline
#3 2009-01-24 2:18 am
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! 
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?
Offline
#4 2009-01-24 1:01 pm
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
Re: Okay, I give. How do I replace a perenthesis in a string in JavaScript
The last one works.
Thanks a lot!
Offline
Pages: 1
- Index
- » Programming & Web Dev
- » Okay, I give. How do I replace a perenthesis in a string in JavaScript
