remove a string from within a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • soni29

    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.
  • Andrew Urquhart

    #2
    Re: remove a string from within a string

    *soni29* wrote:[color=blue]
    > 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?[/color]

    An example of one method:

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

    function Censor(objForm, strElementName) {
    if (objForm && objForm.element s[strElementName]) {
    var objElement = objForm.element s[strElementName];
    var strData = objElement.valu e;
    for (var i=0; i<objCensorWord s.length; ++i) {
    var objRE = new RegExp(objCenso rWords[i], "ig");
    strData = strData.replace (objRE, "");
    }
    objElement.valu e = strData;
    return true;
    }
    else {
    alert("An error message");
    }
    return false;
    }
    </script>
    <form action="page.ex t" method="post"
    onsubmit="retur n Censor(this, 'mydata');">
    <fieldset>
    <legend>Examp le 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/


    Comment

    • Steve van Dongen

      #3
      Re: remove a string from within a string

      "Andrew Urquhart" <useWebsiteInSi gnatureToReply@ spam.invalid> wrote:
      [color=blue]
      >*soni29* wrote:[color=green]
      >> 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?[/color]
      >
      >An example of one method:
      >
      ><script type="text/javascript">
      >var objCensorWords = new Array("ears", "feet", "navel");
      >
      >function Censor(objForm, strElementName) {
      > if (objForm && objForm.element s[strElementName]) {
      > var objElement = objForm.element s[strElementName];
      > var strData = objElement.valu e;
      > for (var i=0; i<objCensorWord s.length; ++i) {
      > var objRE = new RegExp(objCenso rWords[i], "ig");[/color]

      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

      Comment

      • rh

        #4
        Re: remove a string from within a string

        "Andrew Urquhart" wrote:[color=blue]
        > *soni29* wrote:[color=green]
        > > 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?[/color]
        >
        > An example of one method:
        >
        > <script type="text/javascript">
        > var objCensorWords = new Array("ears", "feet", "navel");
        >
        > function Censor(objForm, strElementName) {
        > if (objForm && objForm.element s[strElementName]) {
        > var objElement = objForm.element s[strElementName];
        > var strData = objElement.valu e;
        > for (var i=0; i<objCensorWord s.length; ++i) {
        > var objRE = new RegExp(objCenso rWords[i], "ig");
        > strData = strData.replace (objRE, "");[/color]

        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("+o bjCensorWords.j oin("|")+")\\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

        Comment

        Working...