SitePoint Sponsor |
|
User Tag List
Results 1 to 4 of 4
Thread: help with global replace
-
Nov 7, 2009, 17:06 #1
- 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('\\',''); }
This is what i have at the moment:
Code:String.prototype.stripSlashes = function () { return this.replace('/\\/g',''); }
-
Nov 7, 2009, 19:50 #2
- 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
-
Nov 8, 2009, 03:22 #3
- 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!
-
Dec 15, 2009, 13:41 #4
- 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 %>
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