cancel
Showing results for 
Search instead for 
Did you mean: 

javascript to replace inverted commas

chenks76
All Star
Posts: 3,274
Thanks: 338
Fixes: 12
Registered: ‎24-10-2013

javascript to replace inverted commas

i have a little script that strips out any characters from a text variable.

 

Function Encode(DirtyText)
	Dim CleanText
	Cleantext = Server.HtmlEncode(DirtyText)
	CleanText = Replace(CleanText, "'", "''")
	CleanText = Replace(CleanText, vbCrLf, "<br>")
	Encode = CleanText
End Function

i want it to be able to strip out inverted commas too, ", and replace it with something else>
however 

CleanText = Replace(CleanText, """, ".")

 doesn't work as it doesn't like """.
so does anyone know how to do this?

the end result is used as follows.

newvariable = Encode(cstr(request.form("oldvariable")))
12 REPLIES 12
jelv
Seasoned Hero
Posts: 26,785
Thanks: 971
Fixes: 10
Registered: ‎10-04-2007

Re: javascript to replace inverted commas

Try

CleanText = Replace(CleanText, '"', ".")

or

CleanText = Replace(CleanText, "\"", ".")
jelv (a.k.a Spoon Whittler)
   Why I have left Plusnet (warning: long post!)   
Broadband: Andrews & Arnold Home::1 (FTTC 80/20)
Line rental: Pulse 8 Home Line Rental (£14.40/month)
Mobile: iD mobile (£4/month)
chenks76
All Star
Posts: 3,274
Thanks: 338
Fixes: 12
Registered: ‎24-10-2013

Re: javascript to replace inverted commas

both of those produce syntax errors
Anonymous
Not applicable

Re: javascript to replace inverted commas

Try this :

cleanText = dirtyText.replace(/['"]+/g, '.')
chenks76
All Star
Posts: 3,274
Thanks: 338
Fixes: 12
Registered: ‎24-10-2013

Re: javascript to replace inverted commas

nope

 

Microsoft VBScript compilation error '800a03ea'

Syntax error

/includes/functions.asp, line 44

cleanText = dirtyText.replace(/['"]+/g, '.')
------------------------------^
SpendLessTime
Hero
Posts: 3,000
Thanks: 928
Fixes: 86
Registered: ‎21-09-2009

Re: javascript to replace inverted commas

Try using 

 Chr(34)

as the string to be replaced

Ex - Plusnet Customer (2009 - 2023) now with BT
Anonymous
Not applicable

Re: javascript to replace inverted commas

VBScript so it may need escaping so put a \ in front of the forward slashes. i.e.

(\/['"]+\/g, '.')

Edit:- Just so you know (assuming this works) this will replace all occurrences of a double quote with a .

chenks76
All Star
Posts: 3,274
Thanks: 338
Fixes: 12
Registered: ‎24-10-2013

Re: javascript to replace inverted commas

still nope

 

Microsoft VBScript compilation error '800a03ea'

Syntax error

/staffportal/includes/functions.asp, line 44

cleanText = dirtyText.replace(\/['"]+\/g, '.')
------------------------------^
Anonymous
Not applicable

Re: javascript to replace inverted commas

@chenks76 - No that's fair enough as that's not how regular expressions work in VB land according to my search (but would have for JavaScript). Have a look here it should give you a pointer as to what you need to do, if you want to continue with it.

MJN
Pro
Posts: 1,318
Thanks: 161
Fixes: 5
Registered: ‎26-08-2010

Re: javascript to replace inverted commas


@chenks76 wrote:
[...]
CleanText = Replace(CleanText, """, ".")

 doesn't work as it doesn't like """.
so does anyone know how to do this?


 

The double-quote " is a special character in VBScript and so to use it literally (i.e. without special meaning; just as a character) use two consecutive double-quotes instead. Thus:

 

CleanText = Replace(CleanText, """", ".")

 

(Caveat: I don't know this from first hand experience of VBScript, just a cursory Internet search)

jelv
Seasoned Hero
Posts: 26,785
Thanks: 971
Fixes: 10
Registered: ‎10-04-2007

Re: javascript to replace inverted commas

Sad

It would have helped if your original post had said you were trying to use VBscript and not Javascript!

jelv (a.k.a Spoon Whittler)
   Why I have left Plusnet (warning: long post!)   
Broadband: Andrews & Arnold Home::1 (FTTC 80/20)
Line rental: Pulse 8 Home Line Rental (£14.40/month)
Mobile: iD mobile (£4/month)
MJN
Pro
Posts: 1,318
Thanks: 161
Fixes: 5
Registered: ‎26-08-2010

Re: javascript to replace inverted commas

Did you get anywhere with this @chenks76?

chenks76
All Star
Posts: 3,274
Thanks: 338
Fixes: 12
Registered: ‎24-10-2013

Re: javascript to replace inverted commas

haven't looked at it again yet.
tied up with other stuff right now.