I was looking for a way to replace all special characters with a replace function. I want to use the Razor syntax but this
@Product.Name.Regex.Replace(@"[^A-Za-z0-9/\s/g]", "_")
does not do the trick.
I've tried this
@Regex.Replace(@Product.Name,@"[^A-Za-z0-9/\s/g]", "_")
and it failed as well.
By now I've tried a lot of other things and a lot of times I keep getting this error message: "The name 'Regex' does not exist in the current context"
Can anybody please help?
You need to add @using System.Text.RegularExpressions
to the top of your template. Also, this question will help you if you want this namespace to be available in all templates.
To be complete, the right code for the regular expression is:
@Regex.Replace(@Product.Name,@"[^A-Za-z0-9\.\,_]", "_")
This will replace all special characters with an underscore.
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