SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Jamison DaDaHost's Avatar
    Join Date
    Sep 2002
    Location
    Florida, USA
    Posts
    244
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    String parser that removes puncuation and replaces spaces?

    Can anyone share a string parser that removes puncuation and replaces spaces?

    Example: myParser("Flash Vs. CSS/HTML: Which Will You Choose?")
    Returns: "Flash_Vs_CSSHTML_Which_Will_You_Choose"

    Thanks,
    Jamie

  2. #2
    ********* Janitor levik's Avatar
    Join Date
    Jan 2003
    Location
    New York
    Posts
    885
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suggest you go through the string character by character building a new string:

    if a charactes is a letter or a number, add it to the new string.

    if a character is a space, a '_' to the new string

    otherwise, skip the character

  3. #3
    Jamison DaDaHost's Avatar
    Join Date
    Sep 2002
    Location
    Florida, USA
    Posts
    244
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about using two calls to replace with regular expressions? Something like:

    strNew = strOld.replace(/\.\,\-/, "");
    strNew = strNew.replace(" ","_");

    I'm not sure how the regular expression would look though.

    Jamie

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yet another non-methodical method....
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <title>untitled</title>
    </head>
    <body>
    Example: myParser("Flash Vs. CSS/HTML: Which Will You Choose?") 
    Returns: "Flash_Vs_CSSHTML_Which_Will_You_Choose"
    <script type="text/javascript" language="javascript">
    
    String.prototype.underscore = function()
    {
    	return this.replace(/[\/\?]/g, '').replace(/[^A-Za-z0-9]/g, '_');
    }
    
    var x = 'Flash Vs. CSS/HTML: Which Will You Choose?';
    x = x.underscore();
    alert(x);
    
    </script>
    </body>
    </html>
    The first call to .replace() should specifically match characters you'd like to eliminate. I'm sure this needs some fine (no-so-fine?)-tuning.
    ::: certified wild guess :::

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's my take on it.
    Code:
    String.prototype.customParse = function()
    {
    	return this.replace( /[^A-Za-z0-9 ]/g, "" ).replace( / +/g, "_" );
    }
    BTW, so you both know, it's not necessary to escape metacharacters inside a character set. So, instead of [\/\?], you can just use [/?]
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





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
  •