SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2009
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with global replace

    I'm trying to make a function to strip a string of all \ slashes.

    Initially I had:
    Code:
    String.prototype.stripSlashes = function () 
    {
      return this.replace('\\',''); 
    }
    and that worked fine except it only replaced the first slash (which it apparently is supposed to do). So i read up about global replacements but cant seem to get the right combination of /s and \s.
    This is what i have at the moment:
    Code:
    String.prototype.stripSlashes = function () 
    {
      return this.replace('/\\/g',''); 
    }
    but thats not working at all. any suggestions? thanks!!

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this.replace(/\\/g,'')

    You need to use a regular expression in order to be able to specify a global replace. I used the regex litteral syntax, but you could also have used the RegExp object to create one via a string, and have the opportunity to specify the g flag

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2009
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much for that information - used what you gave me and it worked perfectly, thanks!

  4. #4
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code below results in this:

    --.html
    or
    -.html

    instead of:

    ManufacturerName-ManufacturerPartNumber-PartNumber.html



    Code:
      <%
    
    If Not oRS.EOF Then
    Do While not oRS.eof
    
        MfgName = oRS("MfgName") 
        MfgPart = oRS("MfgPart") 
        PartNo = oRS("PartNo") 
    
    Set Regex = New RegExp 
    Regex.Pattern = "[^A-Za-z0-9]"   ' Basically not alphanumeric
    Regex.Global = True
    
        if len(MfgName) > 0 then 
    		MfgName = Regex.Replace(Trim(Request("MfgName")),"")
    	else
    		MfgName = ""
    	End If
        if len(MfgPart) > 0 then 
    		MfgPart = Regex.Replace(Trim(Request("MfgPart")),"")
    	else
    		MfgPart = ""
    	End If
        if len(PartNo) > 0 then 
    		PartNo = Regex.Replace(Trim(Request("PartNo")),"")
    	else
    		PartNo = ""
    	End If
    	
    
    addforurl=".html"
    
    If PartNo = "" then
    	producturl=MfgName + "-" + MfgPart + addforurl
    Else
    	producturl=MfgName + "-" + MfgPart + "-" + PartNo + addforurl
    End If
    
    %>
    
    <%=producturl%>
    
    <br>
    <%
    oRS.movenext
    Loop
    %>
    Right now I am using this bloated method to strip out unwanted characters:

    Code:
    <%
        MfgPart = oRS("MfgPart") 
        if len(MfgPart) > 0 then 
            MfgPart = replace(MfgPart, " ", "") 
            MfgPart = replace(MfgPart, "/", "") 
            MfgPart = replace(MfgPart, "&", "") 
            MfgPart = replace(MfgPart, "(", "") 
            MfgPart = replace(MfgPart, ")", "") 
            MfgPart = replace(MfgPart, "#", "") 
            MfgPart = replace(MfgPart, "_", "") 
            MfgPart = replace(MfgPart, "|", "") 
            MfgPart = replace(MfgPart, "'", "") 
        else 
            MfgPart = "" 
        end if 
    %>


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
  •