---
title: "Funktion som byter ut i länkar"
type: "forum-thread"
url: "https://www.webforum.nu/amne/asp/137650-funktion-som-byter-ut-i-länkar"
topic: "ASP"
topic_url: "https://www.webforum.nu/amne/asp"
author: "hakan@4u2.se"
published: "2005-11-15T10:08:01.000Z"
updated: "2005-11-17T14:02:10.000Z"
replies: 6
views: 539
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/137650-funktion-som-byter-ut-i-länkar"
---

# Funktion som byter ut i länkar

## #1 — hakan@4u2.se, 2005-11-15T10:08Z

Behöver en funktion som plockar ut och ersätter alla urler i länkar

```
<a href='http://www.aftonbladet.se'>Aftonbladet</a>
<a href='http://www.idg.se'>Idgs hemsida</a> osv
mot
<a href='klick.asp?id=1'>Aftonbladet</a> 
<a href='klick.asp?id=2'>Idgs hemsida</a>
```

Samtidigt som de ersätts så ska de gamla urlerna och länktexten sparas i en array t ex så vet att klick.asp?id=1  är aftonbladet osv

Har en funktion nu som fungerar, men de ändrar bara i urlena inte sparar ner länktexten,har försökt att ändra men inte fått till det med regexp riktigt!!

```
call ReplaceLinks3(Bodytext,textout)
response.write textout

Public Function ReplaceLinks3(Value,textout)
	Dim RegExp
	Dim url
	Dim Match
	Dim Matches

	Set RegExp = New RegExp
	RegExp.Global = True
	RegExp.IgnoreCase = True
	RegExp.Pattern = "<a href\s*=\s*""([^""]*)"""
	Set Matches = RegExp.Execute(Value)
	ReplaceLinks3 = Value
	i = 0
	For Each Match In Matches
		url = Match.SubMatches(0)
		RegExp.Pattern = "(href\s*=\s*"")" & EscapeRegExp(url) & "("")"
		if instr(url,"@") >  0 then
			response.write "<br>Email"
		else
			ReplaceLinks3 = RegExp.Replace(ReplaceLinks3, "$1" &		TranslateURL(url) & "$2")
		end if
		i = i +1
		response.write url&"<br>"
	Next
	textout = ReplaceLinks3
End Function

Function TranslateURL(Value)
'	 sql="insert into Klicks_counter(link) VALUES ('"&Value&"')"
'	 set rs=conn.execute(sql)
'	 sql="select IDENT_CURRENT('Klicks_counter') as ref_klick_id"
'	 set rs=conn.execute(sql)
'	 ref_klick_id = rs("ref_klick_id")

    TranslateURL = "klick.asp?id="&ref_klick_id

End Function
```

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

## #2 — Peter S, 2005-11-15T16:22Z

Testa följande (jag har gjort det enkelt för mig genom att använda korta variabelnamn, nästan enbart gemener o.s.v):

```
function replaceLinks3(v,t)
  dim re
  set re = new regexp

  re.global     = true
  re.ignorecase = true
  re.pattern    = "<a href=(""|')?([^>]+)\1>"

  dim matches
  set matches = re.execute(v)

  if matches.count > 0 then
    dim i, arr()
    i = 1
    redim arr(matches.count + 1)

    for each match in matches
      re.pattern = match
      v = re.replace(v, "<a href=""klick.asp?id=" & i & """>")
      arr(i) = match.submatches(1)

      ' Testing
      response.write "arr(" & i & ") = " & match.submatches(1) & "<br>"

      i = i + 1
    next
  end if

  t = v
end function
```

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

## #3 — hakan@4u2.se, 2005-11-15T16:51Z

Tack Fungerar nästan som jag ville men det saknas en sak!!

Det måste in både url och länktext i array, så att jag senare vet vilken url och länktext som hör samman med resp. klick!

Går det att ordna?   :) 

Typ

```
array_url(2) = "http://www.Aftonbladet.com/"
array_text(2)="Aftonbladets hemsida"
```

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

## #4 — Peter S, 2005-11-16T00:26Z

```
function replaceLinks3(v,t)
  dim re
  set re = new regexp

  re.global     = true
  re.ignorecase = true
  re.pattern    = "<a href=(""|')?([^>]+)\1>(.*?)</a>"

  dim matches
  set matches = re.execute(v)

  if matches.count > 0 then
    dim i, array_url(), array_text()
    i = 1

    redim array_url (matches.count + 1)
    redim array_text(matches.count + 1)

    re.global = false

    for each match in matches
      re.pattern = match
      v = re.replace(v, "<a href=""klick.asp?id=" & i & """>" & match.submatches(2) & "</a>")
      array_url (i) = match.submatches(1)
      array_text(i) = match.submatches(2)

      ' Testing
      response.write "array_url(" & i & ") = " & array_url(i) & "<br>"
      response.write "array_text(" & i & ") = " & array_text(i) & "<br><br>"

      i = i + 1
    next
  end if

  t = v
end function
```

  :)

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

## #5 — hakan@4u2.se, 2005-11-16T13:21Z

Precis så ville jag ha det tack Peter S!

En liten "bugg" bara, när man skickar in html koden nedan så fungerar det inte, den klarar inte av **test.asp?id=123132**
Vad som händer är att den länken inte översätts, utan förblir oändrad, inte så bra!

Finns det någon lösning på det?

```
t= t&"Länk till  <a href="http://www.aftonbladet.se/test.asp?id=123132" target=""_top"">Aftonbladet</a>.Här är <A href=""test.asp"">Test</A>
```

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

## #6 — Peter S, 2005-11-16T15:55Z

```
function replaceLinks3(v,t)
  dim re
  set re = new regexp

  re.global     = true
  re.ignorecase = true
  re.pattern    = "<a href=((?:""|')*)([^'"">]+)\1([^>]*)>(.*?)</a>"

  dim matches
  set matches = re.execute(v)

  if matches.count > 0 then
    dim i, array_url(), array_text()
    i = 1

    redim array_url (matches.count + 1)
    redim array_text(matches.count + 1)

    re.global = false

    for each match in matches
      re.pattern = replace(match,"?","\?")
      v = re.replace(v, "<a href=""klick.asp?id=" & i & """" & match.submatches(2) & ">" & match.submatches(3) & "</a>")
      array_url (i) = match.submatches(1)
      array_text(i) = match.submatches(3)

      ' Testing
      response.write "array_url(" & i & ") = " & array_url(i) & "<br>"
      response.write "array_text(" & i & ") = " & array_text(i) & "<br><br>"

      i = i + 1
    next
  end if

  t = v
end function
```

Jag escape:ar '?' ovan. Du kan även få problem med andra tecken som har speciell betydelse i reguljära uttryck; t.e.x. '+'. Så istället för att ha en replace() i koden ovan, kanske du kan skapa en funktion som hanterar dessa tecken.  :)

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

## #7 — hakan@4u2.se, 2005-11-17T14:02Z

Du är en klippa Peter S! Nu fungerar det prima!! TACK!!   :e

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

---

Tråden på webben: https://www.webforum.nu/amne/asp/137650-funktion-som-byter-ut-i-länkar
