Closed Thread
Results 1 to 4 of 4

Thread: Replace text by ignoring case

  1. #1
    priteshdesai is offline Newbie
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Replace text by ignoring case

    I want to write a program in JS where I can make the text typed by user into short form.
    for example:
    and - &
    are - r
    see you - c u

    I have used the str = str.replace(/ and /g, " & "); method till now but this method wont work for the same text typed in different case.
    eg.
    And, AND, anD wont be converted.
    How can I convert those?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: Replace text by ignoring case

    instead of : str = str.replace(/ and /g, " & ");
    use: str = str.toLowerCase().replace(/ and /g, " & ");
    and theres also "str.toUpperCase();"
    yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  4. #3
    priteshdesai is offline Newbie
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Re: Replace text by ignoring case

    Quote Originally Posted by amrosama View Post
    instead of : str = str.replace(/ and /g, " & ");
    use: str = str.toLowerCase().replace(/ and /g, " & ");
    and theres also "str.toUpperCase();"
    I don't want to change the case of the letters, I just want to change all possible combinations of that word into a different, short word...
    Your suggestion did this:
    Input: and And
    Output: & and
    I wanted this to be the output:
    & &

  5. #4
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: Replace text by ignoring case

    Force the entire string into lowercase, use split to make it into an array.

    Then iterate through the array:

    Code:
    if (arn[i] == "and") {
           document.write("&");
    }
    That is a poor way of doing it though.

    I would use a map structure where the key is what you want to replace, and the value is what you are replacing it with.

    Example:

    Code:
    map.put("and","&");
    map.put("are","r");
    Then you can force the string into lowercase, and iterate through the array. Checking if the word is in the map and if it is make the change.

    I'm not sure if there is a map object in javascript, but that is a quick and easy way to do it.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [jQuery] Characters count ignoring new lines
    By Alhazred in forum JavaScript and CSS
    Replies: 0
    Last Post: 06-06-2011, 05:20 AM
  2. Replies: 5
    Last Post: 10-27-2010, 12:25 AM
  3. [help] C# Replace
    By Darkco in forum C# Programming
    Replies: 7
    Last Post: 12-29-2009, 04:09 PM
  4. replace value in a row to another row
    By jwxie518 in forum PHP Development
    Replies: 2
    Last Post: 07-09-2009, 07:50 PM
  5. Replies: 4
    Last Post: 12-08-2008, 01:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts