12. november 2003 - 09:56Der er
13 kommentarer og 1 løsning
Search and Replace with Regular Expressions
I have a string that contains text and custom tags for pictures. the custom tag looks like this: <picture_x#al>
is it possible via regexp to replace the custom tags with ordinary imagetags where src=picture_x and align=al? there will be no more than 5 custom tags per string and the imagenames will be img1, img2 and so on.
i'm supposed to use this in a news managementsystem... in every article there can be up to 5 images in custom tags mixed in with the articletext like this: word word word word word word word word <bild1#left> word word word word word word word word word word <bild2#right>word word word word word word word word <bild3#left>word word word word word word word word <bild4#center>word word word word word word <bild5#right>.
benkebus your right, the solution would be a regular expressition to do the replace as it support variables, which is needed to find the number and align text.
Try this, it will replace <bildX#vvv> with <img src="picture_X.jpg" align="vvv">
<%
str = "i'm supposed to use this in a news managementsystem... in every article there can be up to 5 images in custom tags mixed in with the articletext like this: word word word word word word word word <bild1#left> word word word word word word word word word word <bild2#right>word word word word word word word word <bild3#left>word word word word word word word word <bild4#center>word word word word word word <bild5#right>." response.Write "Test String:<br>" & str
Set regEx = New RegExp regEx.Global = true regEx.IgnoreCase = true regEx.Pattern = "<bild(\d)#([a-z]*)>" RegEx.Global = true str = RegEx.Replace(str,"<img src=""picture_$1.jpg"" align=""$2"">") Set RegEx = Nothing
That worked fine... if i'd like to reverse the process and replace the <img src="picture_x.jpg" align="align"> back to the original custom tag, how would the regEx.pattern look like then?
help... if "$2"="center" then str = RegEx.Replace(str,"<div align=""$2""><img src=""bilder/upload/bild$1.jpg""></div>") else str = RegEx.Replace(str,"<img src=""bilder/upload/bild$1.jpg"" align=""$2"">") end if
this doesn't work... how should i do it... if a image is centred in the customtag it must be written with <div align=></div>?????
if i'd like to reverse the process and replace the <img src="picture_x.jpg" align="align"> back to the original custom tag, how would the regEx.pattern look like then?
help... if "$2"="center" then str = RegEx.Replace(str,"<div align=""$2""><img src=""bilder/upload/bild$1.jpg""></div>") else str = RegEx.Replace(str,"<img src=""bilder/upload/bild$1.jpg"" align=""$2"">") end if
this doesn't work... how should i do it... if a image is centred in the customtag it must be written with <div align=></div>?????
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.