Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Javascript String Replace

Original Message
Name: chazco
Date: May 26, 2005 at 06:20:58 Pacific
Subject: Javascript String Replace
OS: WinXP
CPU/Ram: 256MB
Comment:
I need to replace PART of a javascript string. The string contains a line of HTML, and i need to replace between two tags in it.

e.g. <html><head>DD</head></html>
Change DD for something else by searching for head. Any ideas how?


Report Offensive Message For Removal


Response Number 1
Name: Chi Happens
Date: May 26, 2005 at 08:17:32 Pacific
Subject: Javascript String Replace
Reply: (edit)
you use IndexOf

Chi

They mostly come at night...mostly


Report Offensive Follow Up For Removal

Response Number 2
Name: chazco
Date: May 26, 2005 at 13:34:01 Pacific
Subject: Javascript String Replace
Reply: (edit)
Thanks Chi. But how do you change the text inside once you've found it. Plus, how do you search wildcards, e.g. search in a string for <body *>*</body>.

Report Offensive Follow Up For Removal

Response Number 3
Name: Dr. Nick
Date: May 26, 2005 at 20:38:00 Pacific
Subject: Javascript String Replace
Reply: (edit)
Use regular expressions.

For example, to replace 'DD' in your first example with 'Howdy', you would do something like:

==================================================================
oldString = "<html><head>DD</head></html>";
newString = oldString.replace(/<head>DD<\/head>/g, "<head>Howdy</head>");

You could use a wildcard so no matter what is between <head> and </head> it gets replaced with 'Howdy' like this:

oldString = "<html><head>DD</head></html>";
newString = oldString.replace(/<head>.*<\/head>/g, "<head>Howdy</head>");

(Make sure to escape any forward slashes in the regular expression [bold above] with a backslash, ie: use <\/head> NOT </head>)

Does that answer your question?


Report Offensive Follow Up For Removal

Response Number 4
Name: chazco
Date: June 13, 2005 at 05:05:28 Pacific
Subject: Javascript String Replace
Reply: (edit)
Thanks everyone. Solved it. (Sorry for delay in replying but had exams)

Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Javascript String Replace

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




Slow boot time

Trasnferring Documents from old HD

My k8T Neo-v usb's aren't working!

Date Modified = Date Created Time

system files on removable harddrive


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC