There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer audio black screen boot bsod computer connection crash css dell display driver drivers email error ethernet excel explorer firefox firefox 3 game hard drive internet internet explorer itunes laptop lcd linux malware network networking nvidia outlook outlook 2003 outlook express partition printer problem router slow software sound trojan usb video virus vista windows windows xp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
javascript search and replace single characters


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
derektech's Avatar
Junior Member with 15 posts.
 
Join Date: Nov 2007
06-Feb-2008, 05:24 AM #1
javascript search and replace single characters
I have been trying to use javascript replace() to search and replace special individual characters in a string. But having some difficulties.

the string is something like:

sometext+othertext&moretext?somemoretext=somevalue&sometext+othertext&moret ext?somemoretext=somevalue

In the above example string I want to replace all occurences of the following characters:

+, =, ?, &

This is how I tried to do it for each character:

replace("+", "NewText").

But this only replaced the first occurence of the character.

I also tried
replace(/+/g, "NewText"), but it would not allow me to do it.


Any assistance will be much appreciated.
artur02's Avatar
Computer Specs
Member with 91 posts.
 
Join Date: Sep 2007
Location: Hungary
Experience: Advanced
06-Feb-2008, 06:05 AM #2
Check this site, especially Example 3 - Global Search.
derektech's Avatar
Junior Member with 15 posts.
 
Join Date: Nov 2007
06-Feb-2008, 06:41 AM #3
Quote:
Originally Posted by artur02 View Post
Check this site, especially Example 3 - Global Search.
thanks, but I have already been to that site. Infact I have the site as a bookmark - the very first place I checked!

The site seems to discuss how to search and replace STRINGS of characters globally.
I need to search and replace SINGLE characters globally.
cristobal03's Avatar
Distinguished Member with 2,992 posts.
 
Join Date: Aug 2005
Experience: Advanced
06-Feb-2008, 02:02 PM #4
If you typed your second attempt literally,
Quote:
replace(/+/g, "NewText")
you need quotes around the plus sign.

I'm guessing this is to swap out literals with entity references and vice versa? If you actually tried replace(/"+"/g, " ") without result, I think we'll need to see a larger chunk of code.

chris.
cristobal03's Avatar
Distinguished Member with 2,992 posts.
 
Join Date: Aug 2005
Experience: Advanced
07-Feb-2008, 02:06 PM #5
[bump]

Boy, was that incorrect. Nevermind what I said about quotes. I don't know what I was thinking. Can we see a larger code sample (if this is still an issue)?

chris.
Mosaic1's Avatar
Distinguished Member with 7,502 posts.
 
Join Date: Aug 2001
07-Feb-2008, 11:24 PM #6
Quote:
The site seems to discuss how to search and replace STRINGS of characters globally.
I need to search and replace SINGLE characters globally.
But a string can be a single character. The length is not the issue. That site is correct.

The + is a special character. So to be read as a string and not as a quantifier , you have to type a backward slash in front of it. Like this \+


So let's say I want to replace all + in the expression:

+ is a character + to be + replaced

with this:
I hate the Plus sign!

Like this:
Code:
var strold = "+ is a character + to be + replaced";
var strnew = strold.replace(/\+/g,"I hate the Plus sign!");
WScript.Echo (strnew);
Mosaic1's Avatar
Distinguished Member with 7,502 posts.
 
Join Date: Aug 2001
07-Feb-2008, 11:29 PM #7
If you want to do this over multiple lines of text, then you need to add the m switch too.
derektech's Avatar
Junior Member with 15 posts.
 
Join Date: Nov 2007
11-Feb-2008, 09:15 AM #8
Thanks Mosaic1.
You're correct. I defined a new string object and remembered to escape the special characters such as ? and +.

It works perfectly.

thanks all for your input.
Mosaic1's Avatar
Distinguished Member with 7,502 posts.
 
Join Date: Aug 2001
12-Feb-2008, 01:21 AM #9
Hi derektech,

You're welcome.

Mo
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:03 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.