I write out a script on my page that looks like this:
<script type="text/javascript">
var test = 'Some user input';
</script>
Some user input comes from my C# code and was previously saved by a user. Now, the problem is, that from time I will get the following error in the browser:
Uncaught SyntaxError: Unexpected token ILLEGAL
I need some generic way of catching these illegal characters and remove them. For example, the following user input actually contains an illegal character:
<p><span>.</span></p>
I've actually had to remove the illegal character here in the Stackoverflow question because it also breaks the editor here - instead I've added the unspoiled example here in jsfiddle: https://jsfiddle.net/p8suow9m/
It's hard to see, but it's there. Looking at it in Google Chrome makes it a little easier to see:

Can someone help me with the right approach in C# to filter away these illegal characters?
Note that I cannot use an approach that uses a list of valid characters and removes everything else, because I do need to support a lot of special characters that are legal in a javascript string.
Try the following c# code, it worked for me: P.S.: Couldn't post it as code, the editor bugs, so I missused the code snippet thing.
Regex.Replace(@"<p><span>test.
> </span></p>", @"[^\u0000-\u007F]", string.Empty);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With