---
title: "replace"
type: "forum-thread"
url: "https://www.webforum.nu/amne/flash/157858-replace"
topic: "Flash"
topic_url: "https://www.webforum.nu/amne/flash"
author: "fakenick!"
published: "2007-03-04T22:42:33.000Z"
updated: "2007-03-04T23:47:24.000Z"
replies: 2
views: 276
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/flash/157858-replace"
---

# replace

## #1 — fakenick!, 2007-03-04T22:42Z

Undrar jag lite på ett script jag hittade här, nämligen:

Kod:
function searchReplace(find,replace) {
	return this.split( find ).join( replace);
}

var hejje = hejje.searchReplace("hej ","");

Vad jag är ute efter är att trimma till texten jag får via XML.. 
jag vill inte ha med vissa ord.. så jag vill replace:a bort det...

Men detta script jag hittade får jag inte att fungera iallafall...
Lägger funktionen längst uppe och lägger sedan till .searchReplace 
i slutet av koden: 

Innan:

Kod:
destination = element.firstChild.nodeValue;

Efter:

Kod:
destination = element.firstChild.nodeValue.searchReplace("hej ","");

detta är ett av mina tappra försök... :-)
Någon som vet bättre än jag?

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

## #2 — fakenick!, 2007-03-04T23:35Z

bytte kod till denna istället som fungerade bra:

```
function searchAndReplace(holder, searchfor, replacement) {
	temparray = holder.split(searchfor);
	holder = temparray.join(replacement);
	return (holder);
}

Usemyreplaced = searchAndReplace("The quick sliver fox jumped over the lazy brown dog", "brown", "orange");
trace(myreplaced);
//traces "The quick sliver fox jumped over the lazy orange dog"
```

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

## #3 — Blixtsystems, 2007-03-04T23:47Z

Flash letar efter en metod som tillhör nodeValue, och den finns inte om du inte lägger till den med hjälp av prototype.
Som det är nu så refererar "this" i funktionen till själva funktionen och inte till någon sträng.

Detta borde funka:

```
function searchReplace(str, find, replace) {
    return str.split(find).join(replace);
}
destination = searchReplace(element.firstChild.nodeValue, "hej ","");
```

EDIT: Lite långsam...

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

---

Tråden på webben: https://www.webforum.nu/amne/flash/157858-replace
