473,394 Members | 1,878 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,394 software developers and data experts.

remove a string from within a string

hi,
how can i remove a string from an existing string in javascript. i
have a textbox in a form and want to make sure that when the user
clicks a button that certain words are moved, like all instances of
"hello" should be taken out of the text the user typed in the textbox.
any ideas?

thank you.
Jul 23 '05 #1
3 21487
*soni29* wrote:
hi,
how can i remove a string from an existing string in javascript. i
have a textbox in a form and want to make sure that when the user
clicks a button that certain words are moved, like all instances of
"hello" should be taken out of the text the user typed in the textbox.
any ideas?


An example of one method:

<script type="text/javascript">
var objCensorWords = new Array("ears", "feet", "navel");

function Censor(objForm, strElementName) {
if (objForm && objForm.elements[strElementName]) {
var objElement = objForm.elements[strElementName];
var strData = objElement.value;
for (var i=0; i<objCensorWords.length; ++i) {
var objRE = new RegExp(objCensorWords[i], "ig");
strData = strData.replace(objRE, "");
}
objElement.value = strData;
return true;
}
else {
alert("An error message");
}
return false;
}
</script>
<form action="page.ext" method="post"
onsubmit="return Censor(this, 'mydata');">
<fieldset>
<legend>Example Form</legend>
<label for="id_mydata">Your data:</label>
<textarea name="mydata" id="id_mydata" cols="20" rows="3">The King has
donkeys' ears</textarea>
<input type="submit" value="Submit">
</fieldset>
</form>

Might be enough to give you an idea of how to implement your own.
Turning off javascript will present a whole new problem. In this example
strings in the 'objCensorWords' array would need to be escaped if they
contain special characters that could be interpretted by the regular
expression engine (unless you specifically wanted that behaviour).
--
Andrew Urquhart
- FAQ: www.jibbering.com/faq/
- Archive: www.google.com/groups?q=comp.lang.javascript
- Contact me: http://andrewu.co.uk/contact/
Jul 23 '05 #2
"Andrew Urquhart" <us**************************@spam.invalid> wrote:
*soni29* wrote:
hi,
how can i remove a string from an existing string in javascript. i
have a textbox in a form and want to make sure that when the user
clicks a button that certain words are moved, like all instances of
"hello" should be taken out of the text the user typed in the textbox.
any ideas?


An example of one method:

<script type="text/javascript">
var objCensorWords = new Array("ears", "feet", "navel");

function Censor(objForm, strElementName) {
if (objForm && objForm.elements[strElementName]) {
var objElement = objForm.elements[strElementName];
var strData = objElement.value;
for (var i=0; i<objCensorWords.length; ++i) {
var objRE = new RegExp(objCensorWords[i], "ig");


That should likely be
var objRE = new RegExp("\b" + objCensorWords[i] + "\b", "ig");
so that you only match it if word boundaries exist on both sides.

Regards,
Steve
Jul 23 '05 #3
rh
"Andrew Urquhart" wrote:
*soni29* wrote:
hi,
how can i remove a string from an existing string in javascript. i
have a textbox in a form and want to make sure that when the user
clicks a button that certain words are moved, like all instances of
"hello" should be taken out of the text the user typed in the textbox.
any ideas?


An example of one method:

<script type="text/javascript">
var objCensorWords = new Array("ears", "feet", "navel");

function Censor(objForm, strElementName) {
if (objForm && objForm.elements[strElementName]) {
var objElement = objForm.elements[strElementName];
var strData = objElement.value;
for (var i=0; i<objCensorWords.length; ++i) {
var objRE = new RegExp(objCensorWords[i], "ig");
strData = strData.replace(objRE, "");


As a note, when in possession of a set of alternates in an array, it's
often more efficient to generate the RegExp as, for example:

re = new RegExp("\\b("+objCensorWords.join("|")+")\\b","ig" )

thereby eliminating the need for a loop of repeated generation of
RegExps and their application to the string. Such a change will bury
the execution within standard regular expression processing.

../rh
Jul 23 '05 #4

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

Similar topics

5
by: jjliu | last post by:
Could someone tell me how to remove all html tags (and anything inside tags) by perl. Some people suggested me to use HTML::TagFilter but i could not find window version. Thanks very much for your...
4
by: francescomoi | last post by:
Hi. I'm trying to remove some characters within a string and substitute others. For instance, I want to convert: John's new house, great ---> Johns-new-house-great I tried with:...
4
by: Bilo | last post by:
I dont know what i am doing false. the code : private void button2_Click(object sender, System.EventArgs e) { foreach (string filename in listBox1.SelectedItems)...
3
by: Don | last post by:
My user control has a combobox with an arraylist attached to it along with custom add and remove methods. The "Add" method is working great. However I don't understand why the "Remove" method...
7
by: Susan Mackay | last post by:
I have a data table that is connected to a database table with a data adapter in the 'standard' manner. However I want to be able to remove selected rows from the data table (i.e. no longer...
37
by: bahoo | last post by:
Hi, I have a list like and as output I want If I myList.remove('0024') then only the first instance of '0024' is removed.
4
by: FullBandwidth | last post by:
I have been perusing various blogs and MSDN pages discussing the use of event properties and the EventHandlerList class. I don't believe there's anything special about the EventHandlerList class in...
3
by: Allen Chen [MSFT] | last post by:
Hi Richard, Quote from Richard================================================== However I also want to be able to remove the panes. I have tried to include this, but find that when I first...
20
by: Nates | last post by:
I have a .bas file saved locally that I load into my Acces project to run a particular sub. I use the following code to load the module (which works fine): I use the following loop to remove...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.