---
title: "RegExp för ersättning av <img>"
type: "forum-thread"
url: "https://www.webforum.nu/amne/asp/163651-regexp-för-ersättning-av-img"
topic: "ASP"
topic_url: "https://www.webforum.nu/amne/asp"
author: "Roosweb"
published: "2007-08-20T12:50:05.000Z"
updated: "2007-08-21T14:50:17.000Z"
replies: 11
views: 669
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/asp/163651-regexp-för-ersättning-av-img"
---

# RegExp för ersättning av <img>

## #1 — Roosweb, 2007-08-20T12:50Z

Hejsan.
Jag satt och gjorde lite såkallade "BB" koder till ett forum, när jag stötte på ett problem, som att göra texten fet och liknande är inget problem det löste jag såhär:

strText = Replace(strText, "\[ b\]", "\<b\>")
strText = Replace(strText, "\[ /b\]", "\</b\>")

Men sen kom jag på att jag vill ha en funktion så att man kan skriva

Permalänk: https://www.webforum.nu/p/163651

## #2 — Peter S, 2007-08-20T16:23Z

```
set re = new regexp
re.global = true
re.pattern = "[img=([^]]+)]"
strText = re.replace(strText, "<img src=""$1"" />")
```

Permalänk: https://www.webforum.nu/p/2018426

## #3 — Roosweb, 2007-08-20T17:08Z

När jag testar det så får jag bara:

Microsoft VBScript runtime error '800a1399' 

Syntax error in regular expression 

includes/codes.asp, line 38 
 
Fast jag har redan en del kod där också, så kanske är den som strular lite? Jag är så dålig på RegExp ännu :)

```
<%
                  
                  Function FixLinks(ByVal strText)
                     With New RegExp
                        .Global=True
                        .IgnoreCase=True
                        .Pattern="(http://)([\w~%&=#:\-/\?\.]*[\w~%&=#:\-/])"
                            strText=.Replace(strText,"<a href=""$1$2"" target=""_blank"">$2</a>")
                            strText = Replace(strText,":)","<img src=""community/Includes/smileys/glad.gif"">")
                            strText = Replace(strText,"><","<img src=""community/Includes/smileys/ahh.gif"">")
                            strText = Replace(strText,":P","<img src=""community/Includes/smileys/P.gif"">")
                            strText = Replace(strText,":(","<img src=""community/Includes/smileys/sur.gif"">")
                            strText = Replace(strText,":-(","<img src=""community/Includes/smileys/grater.gif"">")
                            strText = Replace(strText,";)","<img src=""community/Includes/smileys/blink.gif"">")
                            strText = Replace(strText,":D","<img src=""community/Includes/smileys/superglad.gif"">")
                            strText = Replace(strText,":0","<img src=""community/Includes/smileys/smiley3.gif"">")
                            strText = Replace(strText,":S","<img src=""community/Includes/smileys/smiley5.gif"">")
                            strText = Replace(strText,":@","<img src=""community/Includes/smileys/smiley7.gif"">")

                            strText = Replace(strText,vbcrlf,"<br>")
strText = Replace(strText, "[b]", "<b>")
strText = Replace(strText, "[/b]", "</b>")
strText = Replace(strText, "[c]", "<center>")
strText = Replace(strText, "[/c]", "</center>")
strText = Replace(strText, "[u]", "<u>")
strText = Replace(strText, "[/u]", "</u>")
strText = Replace(strText, "[i]", "<i>")
strText = Replace(strText, "[/i]", "</i>")
strText = Replace(strText, "[red]", "<font color=""#ff0000"">")
strText = Replace(strText, "[/red]", "</font>")
strText = Replace(strText, "[p]", "<p></p>")
strText = Replace(strText, "[br]", "<br />")
strText = Replace(strText, "<embed>", "****")     
strText = Replace(strText, "<script>", "****Jag är en dålig hacker****")
set re = new regexp
re.global = true
re.pattern = "[img=([^]]+)]"
strText = re.replace(strText, "<img src=""$1"" />")
                End With
                         FixLinks=strText
                  End Function

Function Kort(strText,intNumb)
  If Len(strText) > intNumb Then
  Response.Write Left(strText,intNumb)&"..."
  Else
  Response.Write strText
  End If
End Function				  
				  
%>
```

Permalänk: https://www.webforum.nu/p/2018431

## #4 — Peter S, 2007-08-20T17:15Z

Oops!

```
set re = new regexp
re.global = true
re.pattern = "\[img=([^]]+)\]"
strText = re.replace(strText, "<img src=""$1"" />")
```

 :)

Permalänk: https://www.webforum.nu/p/2018432

## #5 — Roosweb, 2007-08-20T17:31Z

Okej ingen mer felkod i alla fall, men bildfunktionen fungerar inte heller, om inte jag skriver fel då, testade med:

Permalänk: https://www.webforum.nu/p/2018437

## #6 — Peter S, 2007-08-20T17:38Z

Det fungerar för mig. Problemet ligger i

```
.Pattern="([url]http://)([/url][\w~%&=#:\-/\?\.]*[\w~%&=#:\-/])"
                            strText=.Replace(strText,"<a href=""$1$2"" target=""_blank"">$2</a>")
```

...som stoppar på \<a\>\</a\>-taggar. Resultatet blir typ

```
<img src="<a href="http://www.roosweb.se/gfx2/roosweb2.jpg" target="_blank">www.roosweb.se/gfx2/roosweb2.jpg</a>" />
```

Permalänk: https://www.webforum.nu/p/2018439

## #7 — Roosweb, 2007-08-20T18:12Z

Okej, jag testade ta bort det då fungerar det, vet du hur man kan komma runt detta så jag kan ha båda funktionerna? Så man både kan skriva http:// och att det automatiskt blir en länk och så att \[img= fungerar?

Tackar för hjälpen :)

Permalänk: https://www.webforum.nu/p/2018448

## #8 — Peter S, 2007-08-20T18:21Z

Någonting sådant här kanske

```
set re = new regexp
re.global = true
re.pattern = "\[img=<a href=""([^""]+)[^]]+\]"
strText = re.replace(strText, "<img src=""$1"" />")
```

Edit: Skrivs alltid hela sökvägen till bilden ut?

Permalänk: https://www.webforum.nu/p/2018451

## #9 — voigtann1, 2007-08-20T18:24Z

```
Sub FixReg(re,pattern,replace,str)
    re.pattern = pattern
    str = re.replace(str,replace)
End Sub

Function FixLinks(ByVal strText)
  Dim Reg
  set Reg = new RegExp
    Reg.Global=True
    Reg.IgnoreCase=True
'    strText = Replace(strText,":(","<img src=""community/Includes/smileys/sur.gif"">")
'    strText = Replace(strText,":-(","<img src=""community/Includes/smileys/grater.gif"">")
'    strText = Replace(strText,";)","<img src=""community/Includes/smileys/blink.gif"">")
'    strText = Replace(strText,":D","<img src=""community/Includes/smileys/superglad.gif"">")
'    strText = Replace(strText,":0","<img src=""community/Includes/smileys/smiley3.gif"">")
'    strText = Replace(strText,":S","<img src=""community/Includes/smileys/smiley5.gif"">")
'    strText = Replace(strText,":@","<img src=""community/Includes/smileys/smiley7.gif"">")
    strText = Replace(strText, "[p]", "<p></p>")
    FixReg Reg, "(^|\W):\)(?=$|\W)", "$1<img src=""community/Includes/smileys/glad.gif"">", strText
    FixReg Reg, "(^|\W)><(?=$|\W)", "$1<img src=""community/Includes/smileys/ahh.gif"">", strText
    FixReg Reg, "(^|\W):P(?=$|\W)", "$1<img src=""community/Includes/smileys/P.gif"">", strText
    FixReg Reg, "\[c\]", "<center>", strText
    FixReg Reg, "\[\/c\]", "</center>", strText
    FixReg Reg, "\[red\]", "<span style=""color:#f00;"">", strText
    FixReg Reg, "\[\/red\]", "</span>", strText
    FixReg Reg, "\[br\]|" & vbCrLf,"<br />", strText
    FixReg Reg, "\<embed\>","****", strText
    FixReg Reg, "\<script\>","****Jag är en dålig hacker****", strText
    FixReg Reg, "[^\=]([url]http://)([/url][\w~%&=#:\-/\?\.]*[\w~%&=#:\-/])", "<a href=""$1$2"" target=""_blank"">$2</a>", strText
    FixReg Reg, "\[(b|\/b|i|\/u|i|\/i)\]", "<$1>", strText
    FixReg Reg, "\[img=([^]]+)\]","<img src=""$1"" />", strText
  FixLinks=strText
End Function
```

Fungerade på det jag testade.. (dock kommentarde jag ut några + gjorde om lite i din funktion)... använd vad du vill..

Permalänk: https://www.webforum.nu/p/2018452

## #10 — Roosweb, 2007-08-20T19:12Z

Tack så sjukt mycket för allt arbete du lade ned på den koden åt mig! Fungerar klockrent, tack så mycket båda två för hjälpen! :)

Permalänk: https://www.webforum.nu/p/2018478

## #11 — voigtann1, 2007-08-21T06:30Z

Glöm inte att att avsluta "Reg" 

```
Set Reg  = Nothing
```

Permalänk: https://www.webforum.nu/p/2018579

## #12 — Roosweb, 2007-08-21T14:50Z

Redan gjort :) Lite kan jag tänka själv i alla fall hehe ;)

Permalänk: https://www.webforum.nu/p/2018667

---

Tråden på webben: https://www.webforum.nu/amne/asp/163651-regexp-för-ersättning-av-img
