cancel
Showing results for 
Search instead for 
Did you mean: 

javascript to replace inverted commas

Community Veteran
Posts: 3,274
Thanks: 339
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
Community Veteran
Posts: 26,718
Thanks: 929
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 (£13/month)
Mobile: iD mobile (£4/month)
Community Veteran
Posts: 3,274
Thanks: 339
Fixes: 12
Registered: 24-10-2013

Re: javascript to replace inverted commas

both of those produce syntax errors
Community Veteran
Posts: 5,439
Thanks: 1,421
Fixes: 34
Registered: 16-10-2014

Re: javascript to replace inverted commas

Try this :

cleanText = dirtyText.replace(/['"]+/g, '.')
Community Veteran
Posts: 3,274
Thanks: 339
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
Aspiring Hero
Posts: 2,597
Thanks: 699
Fixes: 67
Registered: 21-09-2009

Re: javascript to replace inverted commas

Try using 

 Chr(34)

as the string to be replaced

Community Veteran
Posts: 5,439
Thanks: 1,421
Fixes: 34
Registered: 16-10-2014

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 .

Community Veteran
Posts: 3,274
Thanks: 339
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, '.')
------------------------------^
Community Veteran
Posts: 5,439
Thanks: 1,421
Fixes: 34
Registered: 16-10-2014

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
Aspiring Pro
Posts: 1,103
Thanks: 54
Fixes: 2
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)

Community Veteran
Posts: 26,718
Thanks: 929
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 (£13/month)
Mobile: iD mobile (£4/month)
MJN
Aspiring Pro
Posts: 1,103
Thanks: 54
Fixes: 2
Registered: 26-08-2010

Re: javascript to replace inverted commas

Did you get anywhere with this @chenks76?

Community Veteran
Posts: 3,274
Thanks: 339
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.