View Full Version : "Replace" using regular expressions
Joseph McGarvey
03-18-2003, 06:54 PM
Hi -
I am trying to do a global replace on a string. I am unfamiliar with regular expressions... but willing to learn. How can I use RE's to replace a "class" value in a tag, no matter what is in the quotes. The string is HTML content provided by user input. Example (using javascript ASP):
the tag I want to replace would include a wildcard " class="*" "
var theTagToReplace = ??? //Not sure here... class="*"
var replaceTagWithThis = "";
strOUT = theHTMLeditor.innerHTML;
strOUT = strOUT.replace(theTagToReplace, replaceTagWithThis);
return(strOUT);
Thanks!
whammy
03-22-2003, 03:12 AM
Gimme a bit, I will work on this. I have some similar code that will require a bit of tweaking.
Assuming you haven't already figured this out? Please let me know.
Morgoth
03-22-2003, 04:47 AM
So, you're trying to use ASP to find the text inside of a class tag of html?
The html is stored in a variable correct?
Example: Html = "<table bgcolor=""Colour"" class=""*"">"
I don't know exactly what you mean, but if I think I know what your trying to do it's as simple as telling you.
Use the 'Html' variable.
Search that variable for "class=""" and mark where it says the last quotation mark.
When found the number of charaters to the last quotation mark of where it's found, store it in a variable called 'First'.
Search that variable for """ " and mark where it says the first quotation mark.
When found the number of characters to the first quotation mark of where it's found, store it in a variable called 'Last'.
Now grab the text from inside of 'First' and 'Last'; you now have your class text, store it in a variable called 'ClassText'.
Am I right? Is that what you are trying to find so you can replace it with whatever you need?
Now, since you probibly want to add new class text in there, you might want to:
Use the 'Html' variable.
Grab the text from the first character to 'First', store it in the variable 'FirstHtml'.
Next grab the text starting at 'Last' to the end of file, store it in the variable 'LastHtml'.
Take your new text that you replaced 'ClassText' with, and make the html variable equal 'FirstHtml', then 'ClassText', then 'LastHtml'.
This is what you want to do, correct?
Now that we have that sorted out... You want to be able to do this using RE, and not all of those little functions I was explaining?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.