I have a routine that taked content from a content table in the db.
The content is in a string and is intended to be rendered as HTML via Html.Raw.
The content string may (or may not) have a substring in the form of <div class="someclass">.
I want to be able to "go into" the content string, extract the classname that is in quotes within the string (e.g., someclass in the example above), and optionally replace the classname with a different classname (e.g. change the example to read "<div class="someotherclass">)
I can be pretty certain that the <div class=" and the "> parts of the substring will always be exactly as shown (i.e., no gratuitous spaces, no single quote instead of double quote). However, I cannot guarantee that the entire content string might not contain
another substring whose value is "someclass". I can guarantee that the "someclass" for which I am looking will always be in the format shown above (i.e., "<div class="someclass">"
What is the best technique for finding the desired substring within the overall content string and replacing the classname with a different classname?
Thanks, but the case here is that I am working pieces of html, stored in a database, for use later in generating dynamic webpages.
So there is no html document in place at the time I want to do this.
This is a problem involving strings stored in the database and I am manipulating those strings inside a web matrix page whose purpose is to update the database. There is no target html doc
rrrsr7205
Participant
1304 Points
313 Posts
How to change a quoted string within a string
Jun 20, 2012 04:33 PM|LINK
I have a routine that taked content from a content table in the db.
The content is in a string and is intended to be rendered as HTML via Html.Raw.
The content string may (or may not) have a substring in the form of <div class="someclass">.
I want to be able to "go into" the content string, extract the classname that is in quotes within the string (e.g., someclass in the example above), and optionally replace the classname with a different classname (e.g. change the example to read "<div class="someotherclass">)
I can be pretty certain that the <div class=" and the "> parts of the substring will always be exactly as shown (i.e., no gratuitous spaces, no single quote instead of double quote). However, I cannot guarantee that the entire content string might not contain another substring whose value is "someclass". I can guarantee that the "someclass" for which I am looking will always be in the format shown above (i.e., "<div class="someclass">"
What is the best technique for finding the desired substring within the overall content string and replacing the classname with a different classname?
Mudasir.Khan
All-Star
15346 Points
3142 Posts
Re: How to change a quoted string within a string
Jun 20, 2012 04:36 PM|LINK
var data = document.getElementById('id').innerHTML;
document.getElementById('id').innerHTML = data.replace(/"/g,"'");
GmGregori
Contributor
5446 Points
735 Posts
Re: How to change a quoted string within a string
Jun 20, 2012 04:43 PM|LINK
Maybe another solution could be to use Html Agility Pack.
rrrsr7205
Participant
1304 Points
313 Posts
Re: How to change a quoted string within a string
Jun 20, 2012 04:50 PM|LINK
Thanks, but the case here is that I am working pieces of html, stored in a database, for use later in generating dynamic webpages.
So there is no html document in place at the time I want to do this.
This is a problem involving strings stored in the database and I am manipulating those strings inside a web matrix page whose purpose is to update the database. There is no target html doc
Mikesdotnett...
All-Star
154840 Points
19854 Posts
Moderator
MVP
Re: How to change a quoted string within a string
Jun 20, 2012 05:44 PM|LINK
What's wrong with a simple string.Replace?
var content = content.Replace("class=\"someclass\"", "class=\"someotherclass\"");Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
GmGregori
Contributor
5446 Points
735 Posts
Re: How to change a quoted string within a string
Jun 20, 2012 10:10 PM|LINK
Html Agility Pack works on strings too, not only on html documents.
Look at this thread, for example.