473,395 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

string().replace() usage...


Folks,

Can someone help my syntax?

var z=myForm.myInputTag.value.string().replace(" ","-");

I would expect the above to allow z to take the value of an input tag
(called myInputTag) and replace spaces with dashes...

It doesn't change the string - nor do I get errors in the Mozilla
javascript conole...

All help, via the newsgroup (for all to learn) is appreciated, thanks
randelld
Jul 23 '05 #1
4 1438
All form values are already interpretted as "string" data types.
Remove the ".string()" from your statement, and it should work. Note
that this change will only put the updated string into your variable,
not the form, as you currently have the code written.

Make the update, then do an "alert()" on the variable to make sure
everything is cool.

Jul 23 '05 #2
Randell D. wrote:
Folks,

Can someone help my syntax?

var z=myForm.myInputTag.value.string().replace(" ","-");

I would expect the above to allow z to take the value of an input tag (called myInputTag) and replace spaces with dashes...

It doesn't change the string - nor do I get errors in the Mozilla
javascript conole...

All help, via the newsgroup (for all to learn) is appreciated, thanks
randelld


You may be thinking of the Object.toString() method; no such thing as a
..string() method. Also: is 'myForm' simply the name of the form? If so
you'll need to use either document.myForm, or
document.getElementById('myForm') with <form id="myForm"...>.
String.replace works with two string arguments, but it's far more
versatile with a regular expression and a replacement string (or
callback function):

var z = document.myForm.myInputTag.value.replace(/ /g, '-');

If you wanted *one* dash, even for multiple spaces:

var z = document.myForm.myInputTag.value.replace(/ +/g, '-');

Jul 23 '05 #3
RobB wrote:
Randell D. wrote:
Folks,

Can someone help my syntax?

var z=myForm.myInputTag.value.string().replace(" ","-");

I would expect the above to allow z to take the value of an input tag


(called myInputTag) and replace spaces with dashes...

It doesn't change the string - nor do I get errors in the Mozilla
javascript conole...

All help, via the newsgroup (for all to learn) is appreciated, thanks
randelld

You may be thinking of the Object.toString() method; no such thing as a
.string() method. Also: is 'myForm' simply the name of the form? If so
you'll need to use either document.myForm, or
document.getElementById('myForm') with <form id="myForm"...>.
String.replace works with two string arguments, but it's far more
versatile with a regular expression and a replacement string (or
callback function):

var z = document.myForm.myInputTag.value.replace(/ /g, '-');

If you wanted *one* dash, even for multiple spaces:

var z = document.myForm.myInputTag.value.replace(/ +/g, '-');

Thanks for that - I had used a regexp as I didn't believe it was
required... now it works...

Cheers
Randell D.
Jul 23 '05 #4
Spats30 wrote:
All form values are already interpretted as "string" data types.
Remove the ".string()" from your statement, and it should work. Note
that this change will only put the updated string into your variable,
not the form, as you currently have the code written.

Make the update, then do an "alert()" on the variable to make sure
everything is cool.


Yeah, I had tried sending the output via alert() and even tried with and
without string. I don't know why but when I replaced my arguements in
replace() with a regular expression (suggested in a follow on post from
yours) it worked...

Thanks though,
Randell D.
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Niels Bosboom | last post by:
Hi! I am using PHP4.3.3 on a windows XP platform. I am building a small program that will allow my students to subscribe and unsubscribe from a service that will let them know when new results...
4
by: beliavsky | last post by:
The code for text in open("file.txt","r"): print text.replace("foo","bar") replaces 'foo' with 'bar' in a file, but how do I avoid changing text inside single or double quotes? For making...
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
6
by: rithesh.rg | last post by:
Hello, Given below is a part of my code. In the code i am trying to parse through a string and replace all "+" characters by "*". string enc_msg = Convert.ToBase64String(full_enc); for (i...
1
by: shapper | last post by:
Hello, I have a XLST file where I have the phrase "HERE". Something like: <xsl:text>HERE</xsl:text> I want to replace "HERE" by "UPDATED" and save the file.
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
5
by: V S Rawat | last post by:
I was trying to use back-to-back replace functions to convert a url: str1 = str.replace("%2F","/").replace("%3F","?").replace("%3D","=").replace("%2 6","&"); It didn't replace all 4 types of...
16
by: yu_kuo | last post by:
Is there any comparison data on perfomance difference between std::string and c style string? Or maybe if there are source code which could be used to measuer on different compiler/platform, in a...
3
by: jacob navia | last post by:
Abstract: Continuing the discussion about abstract data types, in this discussion group, a string collection data type is presented, patterned after the collection in C# and similar languages...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.