This question already has answers here:
Regex escape with \ or \\?
(5 answers)
Convert C# string to JavaScript String
(3 answers)
Closed 4 years ago.
Salaam
I am looking for a proper version of a C# or Razor equivalent of PHP's addSlashes. That would add
\ to some\string => some\\string
Please provide help
Why I needed this
In my application a user entered Sometext in textbox was accidently pressed next time when page when data was populated though Razor it was like this
...append('<span>'+'#Model.value'+'</span>')
=> after compiling it becomes like this
...append('<span>'+'sometext\'+'</span>')
so with this scenario my javascript code broke at '\' because now single quote has started but not ending due to ``. So i thought instead of limiting characters i would rather add slashes through C# code
Thank You
You don't show any code you've already written, but this can be done by using [string.replace()] ( https://www.w3schools.com/jsref/jsref_replace.asp ) :
var str = "This is \\a test";
var replaced = str.replace("\\", "\\\\");
Whoops - you want the answer in C# , I misread your "javascript" tag. It's mostly the same:
string str = "This is \\a test";
string replaced = str.Replace("\\", "\\\\");
Also see C# String Replace
After the update, https://stackoverflow.com/a/27574931/34092 is most likely a much better answer.
Related
This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 5 years ago.
I have this String
link="https%3a%2f%2fen.wikipedia.org%2fwiki%2fHuawei/"
which shoud be like this:
link="https://en.wikipedia.org/wiki/Huawei/"
I wrote this code:
link.Replace("%2f", "/");
link.Replace("%3a", ":");
But it did not work.
Instead of trying to decode the URL yourself I'd use HttpUtility.UrlDecode
HttpUtility.UrlDecode("https%3a%2f%2fen.wikipedia.org%2fwiki%2fHuawei/")
"https://en.wikipedia.org/wiki/Huawei/"
See: https://msdn.microsoft.com/en-us/library/system.web.httputility.urldecode(v=vs.110).aspx
String.Replace does return the value replaced try:
link = link.Replace("%2f", "/");
link is a string and is not mutating when you call the Replace method
link.Replace will not affect the link object itself, instead it returns a new String
from the doc emphasis mine:
Returns a new string in which all occurrences of a specified Unicode
character in this instance are replaced with another specified Unicode
character.
do instead:
link = link.Replace("%2f", "/"); or
link = link.Replace("%3a", ":");
This question already has answers here:
Split a string by another string in C#
(11 answers)
Closed 5 years ago.
In this scenario the data I could have in my string may look like below but keep in mind the ids are dynamically generated so this isn't static and could be more than 2 if you haven't caught onto that.
ing:server blah blah, you. 2019,;:10-!gs.csd
1. id=value, otherid=value, pos=(22,22,33)
2. id=value2, otherid=value2, pos=(2g,2g,f) info other info info info info etc etc.
EDIT: How am I supposed to extract the individual values into strings afterwards from the string, the following does not work:
String valueString = "csd 1. id=value, otherid=value, pos=(22,22,33) ";
String value = valueString.Substring(valueString.IndexOf("otherid"), valueString.IndexOf(",") - valueString.IndexOf("otherid"));
You can do with Substring since you have already way of expecting when to start and when to end on your searching.
string result = x.Substring(x.IndexOf("csd"), (x.IndexOf("info ") - x.IndexOf("csd")));
I start searching on the start of the word "csd" and ends with the word "info " (with space), since there is also a word of info at the beginning of your string.
The result would be:
"csd 1. id=value, otherid=value, pos=(22,22,33) 2. id=value2, otherid=value2, pos=(24,21,33) "
This question already has answers here:
How can I strip HTML tags from a string in ASP.NET?
(14 answers)
Closed 7 years ago.
I have a string defined like so:
private const String REFER_TO_BUSINESS = "<pre> (Refer to business office for guidance and explain below the circumstances for exception to policy or attach a copy of request)</pre>";
...which has, as you can see, the "pre" tag to preserve the space prepended to the verbiage. I want to, though, reference this string without the "pre" tags. It would be easy enough to search for "<pre>" and "</pre>" and remove them, but it would quickly become tedious to do that with every HTML tag type.
How can I, in C#, strip all tags out of a string, regardless of whether they are "<pre>", "<h1>", "<span>", "<aside>" or anything else?
Try a regex replacement.
This pattern matches html tags within a string. From here
var pattern = #"</?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>";
var source = "<pre> (Refer to business office for guidance and explain below the circumstances for exception to policy or attach a copy of request)</pre>";
Regex.Replace(source, pattern, string.Empty);
This should do what you need it to do:
string stripMeOfHTML = Regex.Replace(stripMeOfHTML, #"<[^>]+>", "").Trim();
This works:
// For strings that have embedded HTML tags for presentation on the form (such as "<pre>" and such), but need to be rendered free of these (such as on the PDF)
private String RemoveHTMLTags(String stringContainingHTMLTags)
{
String regexified = Regex.Replace(stringContainingHTMLTags, "<.*?>", string.Empty);
return regexified;
}
This question already has answers here:
Escaping a double-quote in inline c# script within javascript
(3 answers)
Closed 9 years ago.
I have a C# string that needs to be passed to the page and then sent back to the server. It needs to be able to handle single quotes, double quotes and any other standard characters that might cause problems. I'm using MVC4 and razor. Right now I'm passing the string to javascript. I have something like this:
var str = '#Html.Raw(Model.SomeString.Replace("'", #"\'"))';
This works just fine but I was wondering if there's a better, more elegant way of escaping a C# string that will be passed to javascript.
I've already tried storing the value in a hidden field, like this:
#Html.Hidden(x => x.SomeString)
but then it has problems with double quotes when I go to grab the value.
EDIT: turns out the Html.Hidden issue was being caused by something else
Any help is greatly appreciated!
NOTE: must work in IE9 and 10. No ViewBags.
You have to use HttpUtility.JavaScriptStringEncode. This is made for this exact purpose, it doesn't only encode quotes, but also escape sequences.
And BTW #Html.Hidden should work just fine because it also deals with escaping (a different kind than the above, HTML escaping in this case, dealing with ampersands, < >, etc).
You can use HtmlEncode and the decode counterpart.
In javascript you'll have to do the same: HTML-encoding lost when attribute read from input field
This question already has answers here:
C# regex pattern to extract urls from given string - not full html urls but bare links as well
(3 answers)
Closed 9 years ago.
So far I have
messageText1 = Regex.Replace(messageText1, "(www|http|https)*?(com|.co.uk|.org)", "[URL OMITTED]");
With only the www, and without the bracks or http or https it works as intended
For example and input of Hey check out this site, www.google.com, it's really cool would output hey check out this site, [URL OMITTED], it's really cool
But if I put back in the or operators for the start of the URL, it only replaces the .com part of the input
Why won't it work?
Thanks
(www|http|https)*?(com|.co.uk|.org)
means www or http or https 0 to many times immediately followed by com .co.uk or .org.
So it would match for example httphttphttp.co.uk
Your intention was probably just to have a . before the *. Which then means it only looks for (www|http|https) once, then it matchs . (any character) 0 to many times.
You are also missing the . in .com. However, if you want to match a literal . you need to use \., since a . on its own means 'any character'.
With that in mind, the regex I think you were going for is:
(www|http|https).*?(\.com|\.co\.uk|\.org)
This should work better. It will also work for other TLDs that don't end with .com, .co.uk or .org:
messageText1 = Regex.Replace(messageText1, #"\b(?:http://|https://|www\.)\S+", "[URL OMITTED]");
Your expression is missing a . somewhere or (possibly better) a \S+
(www|http|https)\S*(com|\.co\.uk|\.org)
In C#:
Regex.Replace(messageText1, #"(www|http|https)\S*(com|\.co\.uk|\.org)", "[URL OMITTED]");
Note: you probably want to escape the .'s as well.
A simple version which i tried is as follows.
messageText1 = Regex.Replace(messageText1, #"(www)?(.)?[a-z]*.(com)", "[URL OMITTED]");
i tried this with
string messageText1 = " Hey check this out, http:\www.google.com,its cool";
string messageText1 = " Hey check this out, www.google.com,its cool";
string messageText1 = " Hey check this out, google.com,its cool";