Why isn’t my regular expression finding my string?
Regex.Replace(" SCRIPT language=Javascript src=\"\"\r\ntype=text/javascript ",
"^language=Javascript$",
"language=\"Javascript\"",
RegexOptions.Multiline | RegexOptions.Compiled
);
the ^language=Javascript$ isn’t working. if I remove it it works, but I want to make sure that a partially similar string doesn’t get replaced (it has happened in my tests). The strings are not constants, I actually using variables, but this is just 1 case that I am using. Any help?
Well, this is because
^represents the start of the string and$the end of the string. Your regex simply cannot match. You could useas a workaround if you just want to make sure that
foolanguage=Javascriptdoesn’t match. But that depends a little on the data you expect and stuff into that method. A safer way would bewhich would make sure that there is a space before and after the match. You can make it more complex (and reliable) from there, e.g.
to match even
<script type='text/javascript' language=Javascript>.