How can I replace all occurance of a string except when it's enclosed with a special tag?
-
Hello, Let's say I have a variable named: someVar which contains the following:
href wow nice this <a href="cp.com">cow duck john</a> text etc
And I want to replace: "href" with "link". I can't do:someVar.Replace("href", "link");
Because that will change:<a href="cp.com">
! So, how can I replace all the occurances of "href" when it's not part of an HTML tag? Please help. -
Hello, Let's say I have a variable named: someVar which contains the following:
href wow nice this <a href="cp.com">cow duck john</a> text etc
And I want to replace: "href" with "link". I can't do:someVar.Replace("href", "link");
Because that will change:<a href="cp.com">
! So, how can I replace all the occurances of "href" when it's not part of an HTML tag? Please help.hi for something simple like that just try this: someVar = someVar.Replace("
-
Hello, Let's say I have a variable named: someVar which contains the following:
href wow nice this <a href="cp.com">cow duck john</a> text etc
And I want to replace: "href" with "link". I can't do:someVar.Replace("href", "link");
Because that will change:<a href="cp.com">
! So, how can I replace all the occurances of "href" when it's not part of an HTML tag? Please help.Try this REGEX, I hope it will be useful. You can use any variable instead of link word in the following snippet.
string ResultString = null; try { ResultString = Regex.Replace(someVar, "(>.*?)(href)(.*?<)", "$1link$3", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Multiline); } catch (ArgumentException ex) { // Syntax error in the regular expression }