zwei files
This commit is contained in:
@@ -1,462 +0,0 @@
|
|||||||
# JavaScript String-Methoden und -Eigenschaften – Notizen
|
|
||||||
|
|
||||||
## 1. `length` (Eigenschaft)
|
|
||||||
|
|
||||||
Gibt die Anzahl der Zeichen in einem String zurück.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
const text = "Hallo";
|
|
||||||
console.log(text.length); // 5
|
|
||||||
|
|
||||||
const empty = "";
|
|
||||||
console.log(empty.length); // 0
|
|
||||||
|
|
||||||
const withSpaces = "Hallo Welt";
|
|
||||||
console.log(withSpaces.length); // 11 (Leerzeichen zählt mit)
|
|
||||||
```
|
|
||||||
|
|
||||||
**Wichtig**: `length` ist eine Eigenschaft, keine Methode – keine Klammern!
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 2. `indexOf()` und `lastIndexOf()`
|
|
||||||
|
|
||||||
### `indexOf(searchValue, fromIndex)`
|
|
||||||
Sucht ein Zeichen oder Substring von links nach rechts. Gibt den **Index** (Position) zurück. Wenn nicht gefunden: **-1**.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
const text = "Hallo Welt";
|
|
||||||
|
|
||||||
// Einzelnes Zeichen
|
|
||||||
console.log(text.indexOf("H")); // 0 (erstes Zeichen)
|
|
||||||
console.log(text.indexOf("o")); // 4 (erste "o")
|
|
||||||
|
|
||||||
// Substring
|
|
||||||
console.log(text.indexOf("Welt")); // 6
|
|
||||||
console.log(text.indexOf("xyz")); // -1 (nicht gefunden)
|
|
||||||
|
|
||||||
// Optional: Startposition angeben
|
|
||||||
console.log(text.indexOf("l", 3)); // 3 (erste "l" ab Position 3)
|
|
||||||
```
|
|
||||||
|
|
||||||
### `lastIndexOf(searchValue, fromIndex)`
|
|
||||||
Sucht von rechts nach links – gibt **letztes** Vorkommen zurück.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
const text = "Hallo Welt";
|
|
||||||
|
|
||||||
console.log(text.lastIndexOf("o")); // 9 (letzte "o")
|
|
||||||
console.log(text.lastIndexOf("l")); // 9 (letzte "l")
|
|
||||||
console.log(text.lastIndexOf("Welt")); // 6
|
|
||||||
console.log(text.lastIndexOf("xyz")); // -1 (nicht gefunden)
|
|
||||||
|
|
||||||
// Optional: Position von rechts
|
|
||||||
const text2 = "banana";
|
|
||||||
console.log(text2.lastIndexOf("a", 4)); // 3 (letzte "a" bis Position 4)
|
|
||||||
```
|
|
||||||
|
|
||||||
**Unterschied**:
|
|
||||||
- `indexOf()` → erstes Vorkommen von links
|
|
||||||
- `lastIndexOf()` → letztes Vorkommen von rechts
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 3. `search()`
|
|
||||||
|
|
||||||
Sucht mit **regulärem Ausdruck** (Regex). Gibt Index des ersten Matches zurück, sonst **-1**.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
const text = "Hallo123Welt";
|
|
||||||
|
|
||||||
// Einfache Suche nach Ziffer
|
|
||||||
console.log(text.search(/\d/)); // 5 (erste Ziffer)
|
|
||||||
|
|
||||||
// Groß-/Kleinschreibung ignorieren
|
|
||||||
console.log(text.search(/welt/i)); // 8
|
|
||||||
|
|
||||||
// Mehrere Zeichen
|
|
||||||
console.log(text.search(/[0-9]+/)); // 5
|
|
||||||
|
|
||||||
// Nicht gefunden
|
|
||||||
console.log(text.search(/xyz/)); // -1
|
|
||||||
```
|
|
||||||
|
|
||||||
**Unterschied zu indexOf()**:
|
|
||||||
- `indexOf()` → exakte String-Suche, einfach und schnell
|
|
||||||
- `search()` → Regex-Suche, mächtiger und komplexer
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 4. `slice(start, end)`
|
|
||||||
|
|
||||||
Extrahiert einen Teil des Strings. `end` ist **exklusiv** (wird nicht mit einbezogen).
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
const text = "Hallo Welt";
|
|
||||||
// 01234567890
|
|
||||||
|
|
||||||
// slice(start)
|
|
||||||
console.log(text.slice(0)); // "Hallo Welt" (ab Position 0)
|
|
||||||
console.log(text.slice(6)); // "Welt" (ab Position 6)
|
|
||||||
|
|
||||||
// slice(start, end)
|
|
||||||
console.log(text.slice(0, 5)); // "Hallo" (Position 0 bis 4)
|
|
||||||
console.log(text.slice(6, 10)); // "Welt" (Position 6 bis 9)
|
|
||||||
|
|
||||||
// Negative Indizes (von hinten)
|
|
||||||
console.log(text.slice(-4)); // "Welt" (letzte 4 Zeichen)
|
|
||||||
console.log(text.slice(0, -5)); // "Hallo " (alles außer letzten 5)
|
|
||||||
console.log(text.slice(-4, -1)); // "Wel" (2. bis 4. Zeichen von hinten)
|
|
||||||
```
|
|
||||||
|
|
||||||
**Wichtig**: `slice()` erstellt einen **neuen String**, der ursprüngliche wird nicht verändert!
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 5. `substring(start, end)`
|
|
||||||
|
|
||||||
Ähnlich wie `slice()`, mit kleinen Unterschieden:
|
|
||||||
- `end` ist auch **exklusiv**
|
|
||||||
- Unterstützt **keine negativen Indizes** (werden als 0 interpretiert)
|
|
||||||
- Vertauscht automatisch `start` und `end`, wenn `start > end`
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
const text = "Hallo Welt";
|
|
||||||
|
|
||||||
// Normal
|
|
||||||
console.log(text.substring(0, 5)); // "Hallo"
|
|
||||||
console.log(text.substring(6, 10)); // "Welt"
|
|
||||||
|
|
||||||
// Ohne end (bis zum Ende)
|
|
||||||
console.log(text.substring(6)); // "Welt"
|
|
||||||
|
|
||||||
// Negative Werte → werden 0
|
|
||||||
console.log(text.substring(-5, 5)); // "Hallo" (negativ ignoriert)
|
|
||||||
|
|
||||||
// Vertauschte Parameter
|
|
||||||
console.log(text.substring(5, 0)); // "Hallo" (Parameter werden vertauscht)
|
|
||||||
```
|
|
||||||
|
|
||||||
**Unterschied zu slice()**:
|
|
||||||
- `slice()` → unterstützt negative Indizes, logischer
|
|
||||||
- `substring()` → keine negativen Indizes, bei Bedarf Parameter vertauschen
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 6. `substr()` (Veraltet!)
|
|
||||||
|
|
||||||
Extrahiert Zeichen basierend auf **Startposition** und **Länge** (nicht Ende).
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
const text = "Hallo Welt";
|
|
||||||
|
|
||||||
console.log(text.substr(0, 5)); // "Hallo" (ab Position 0, Länge 5)
|
|
||||||
console.log(text.substr(6, 4)); // "Welt" (ab Position 6, Länge 4)
|
|
||||||
console.log(text.substr(-4)); // "Welt" (letzte 4 Zeichen)
|
|
||||||
console.log(text.substr(-4, 2)); // "We" (ab -4, Länge 2)
|
|
||||||
```
|
|
||||||
|
|
||||||
**Warnung**: `substr()` ist **deprecated** (veraltet). Nutzen Sie stattdessen `slice()` oder `substring()`!
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 7. `replace(searchValue, replaceValue)`
|
|
||||||
|
|
||||||
Ersetzt das **erste** Vorkommen eines Strings/Regex durch einen anderen.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
const text = "Hallo Hallo";
|
|
||||||
|
|
||||||
// Einfacher String
|
|
||||||
console.log(text.replace("Hallo", "Hi"));
|
|
||||||
// "Hi Hallo" (nur erstes Vorkommen!)
|
|
||||||
|
|
||||||
// Mit Regex und "g" Flag (global → alle)
|
|
||||||
console.log(text.replace(/Hallo/g, "Hi"));
|
|
||||||
// "Hi Hi" (alle Vorkommen)
|
|
||||||
|
|
||||||
// Groß-/Kleinschreibung ignorieren
|
|
||||||
console.log(text.replace(/hallo/i, "Hi"));
|
|
||||||
// "Hi Hallo"
|
|
||||||
```
|
|
||||||
|
|
||||||
**Wichtig**:
|
|
||||||
- `replace()` ersetzt nur das **erste** Vorkommen!
|
|
||||||
- Für alle: Regex mit `g` Flag verwenden
|
|
||||||
- Ursprünglicher String wird nicht verändert (neuer String wird zurückgegeben)
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
const result = "abc abc".replace(/abc/g, "xyz");
|
|
||||||
console.log(result); // "xyz xyz"
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 8. `toUpperCase()` und `toLowerCase()`
|
|
||||||
|
|
||||||
Wandelt String in GROSSBUCHSTABEN oder kleinbuchstaben um.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
const text = "Hallo Welt";
|
|
||||||
|
|
||||||
// Großbuchstaben
|
|
||||||
console.log(text.toUpperCase()); // "HALLO WELT"
|
|
||||||
console.log("xyz".toUpperCase()); // "XYZ"
|
|
||||||
|
|
||||||
// Kleinbuchstaben
|
|
||||||
console.log(text.toLowerCase()); // "hallo welt"
|
|
||||||
console.log("XYZ".toLowerCase()); // "xyz"
|
|
||||||
|
|
||||||
// Mit Zahlen und Sonderzeichen
|
|
||||||
console.log("123!@#".toUpperCase()); // "123!@#" (keine Änderung)
|
|
||||||
```
|
|
||||||
|
|
||||||
**Wichtig**: Original wird nicht verändert!
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
const original = "Test";
|
|
||||||
const upper = original.toUpperCase();
|
|
||||||
console.log(original); // "Test" (unverändert!)
|
|
||||||
console.log(upper); // "TEST"
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 9. `concat(...strings)`
|
|
||||||
|
|
||||||
Verbindet mehrere Strings zu einem neuen String.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
const str1 = "Hallo";
|
|
||||||
const str2 = "Welt";
|
|
||||||
|
|
||||||
// Mit concat()
|
|
||||||
console.log(str1.concat(" ", str2)); // "Hallo Welt"
|
|
||||||
console.log("a".concat("b", "c", "d")); // "abcd"
|
|
||||||
|
|
||||||
// Mit mehreren Argumenten
|
|
||||||
console.log("Hello".concat(" ", "World", "!")); // "Hello World!"
|
|
||||||
```
|
|
||||||
|
|
||||||
**Gleich wie `+` Operator**:
|
|
||||||
```javascript
|
|
||||||
const text = "Hallo" + " " + "Welt"; // "Hallo Welt"
|
|
||||||
const text2 = "Hallo".concat(" ", "Welt"); // "Hallo Welt"
|
|
||||||
```
|
|
||||||
|
|
||||||
**Besser**: In der Praxis ist der `+` Operator oder Template Literals üblicher:
|
|
||||||
```javascript
|
|
||||||
const str1 = "Hallo";
|
|
||||||
const str2 = "Welt";
|
|
||||||
|
|
||||||
// + Operator
|
|
||||||
const result1 = str1 + " " + str2;
|
|
||||||
|
|
||||||
// Template Literals (empfohlen)
|
|
||||||
const result2 = `${str1} ${str2}`;
|
|
||||||
|
|
||||||
// concat() (seltener)
|
|
||||||
const result3 = str1.concat(" ", str2);
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 10. `trim()`
|
|
||||||
|
|
||||||
Entfernt Whitespace (Leerzeichen, Tabs, Zeilenumbrüche) vom **Anfang** und **Ende** eines Strings.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
const text = " Hallo Welt ";
|
|
||||||
|
|
||||||
console.log(text.trim()); // "Hallo Welt" (Außenleerzeichen weg)
|
|
||||||
console.log(text.length); // 14
|
|
||||||
console.log(text.trim().length); // 11
|
|
||||||
|
|
||||||
// Mit Tabs und Zeilenumbrüchen
|
|
||||||
const messy = "\t\n Hallo \n\t";
|
|
||||||
console.log(messy.trim()); // "Hallo"
|
|
||||||
|
|
||||||
// Leerzeichen in der Mitte bleiben
|
|
||||||
const text2 = "Hallo Welt";
|
|
||||||
console.log(text2.trim()); // "Hallo Welt" (Mitte unverändert)
|
|
||||||
```
|
|
||||||
|
|
||||||
**Wichtig**: `trim()` entfernt nur am **Anfang** und **Ende**, nicht in der Mitte!
|
|
||||||
|
|
||||||
**Verwandte Methoden**:
|
|
||||||
```javascript
|
|
||||||
const text = " Hallo ";
|
|
||||||
|
|
||||||
// trim() – beide Seiten
|
|
||||||
console.log(text.trim()); // "Hallo"
|
|
||||||
|
|
||||||
// trimStart() oder trimLeft() – nur Anfang
|
|
||||||
console.log(text.trimStart()); // "Hallo "
|
|
||||||
|
|
||||||
// trimEnd() oder trimRight() – nur Ende
|
|
||||||
console.log(text.trimEnd()); // " Hallo"
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 11. `charAt(index)`
|
|
||||||
|
|
||||||
Gibt das Zeichen an einer bestimmten **Position** zurück. Wenn Index ungültig, leerer String `""`.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
const text = "Hallo";
|
|
||||||
// 01234
|
|
||||||
|
|
||||||
console.log(text.charAt(0)); // "H"
|
|
||||||
console.log(text.charAt(1)); // "a"
|
|
||||||
console.log(text.charAt(4)); // "o"
|
|
||||||
console.log(text.charAt(10)); // "" (zu weit, leerer String)
|
|
||||||
console.log(text.charAt(-1)); // "" (negative Index erlaubt nicht)
|
|
||||||
```
|
|
||||||
|
|
||||||
**Alternativ: Bracket-Notation** (einfacher):
|
|
||||||
```javascript
|
|
||||||
const text = "Hallo";
|
|
||||||
|
|
||||||
console.log(text[0]); // "H"
|
|
||||||
console.log(text[4]); // "o"
|
|
||||||
console.log(text[10]); // undefined (anders als charAt!)
|
|
||||||
```
|
|
||||||
|
|
||||||
**Unterschied**:
|
|
||||||
- `charAt(index)` → gibt `""` zurück bei ungültigem Index
|
|
||||||
- `text[index]` → gibt `undefined` zurück bei ungültigem Index
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Vergleich und Zusammenfassung
|
|
||||||
|
|
||||||
| Methode | Zweck | Rückgabewert |
|
|
||||||
|---------|-------|------------|
|
|
||||||
| `length` | Zeichenanzahl | Zahl |
|
|
||||||
| `indexOf()` | Erste Position eines Substrings | Index oder -1 |
|
|
||||||
| `lastIndexOf()` | Letzte Position eines Substrings | Index oder -1 |
|
|
||||||
| `search()` | Position eines Regex-Matches | Index oder -1 |
|
|
||||||
| `slice()` | Extrahiert Teil (Anfang bis Ende, exklusiv) | Neuer String |
|
|
||||||
| `substring()` | Wie slice, aber ohne negative Indizes | Neuer String |
|
|
||||||
| `substr()` | Extrahiert Teil (Position + Länge) | Neuer String (**veraltet!**) |
|
|
||||||
| `replace()` | Ersetzt erstes Vorkommen | Neuer String |
|
|
||||||
| `toUpperCase()` | In Großbuchstaben | Neuer String |
|
|
||||||
| `toLowerCase()` | In Kleinbuchstaben | Neuer String |
|
|
||||||
| `concat()` | Verbindet Strings | Neuer String |
|
|
||||||
| `trim()` | Entfernt äußere Whitespaces | Neuer String |
|
|
||||||
| `charAt()` | Zeichen an Position | Einzelnes Zeichen oder `""` |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Praktische Beispiele
|
|
||||||
|
|
||||||
### Validierung einer E-Mail
|
|
||||||
```javascript
|
|
||||||
const email = " user@example.com ";
|
|
||||||
const cleaned = email.trim().toLowerCase();
|
|
||||||
|
|
||||||
if(cleaned.indexOf("@") > 0 && cleaned.indexOf(".") > cleaned.indexOf("@")){
|
|
||||||
console.log("Gültige E-Mail");
|
|
||||||
} else {
|
|
||||||
console.log("Ungültige E-Mail");
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Dateiname aus Pfad extrahieren
|
|
||||||
```javascript
|
|
||||||
const path = "C:/Users/max/dokumente/file.txt";
|
|
||||||
|
|
||||||
// Letzten Schrägstrich finden
|
|
||||||
const lastSlash = path.lastIndexOf("/");
|
|
||||||
const filename = path.slice(lastSlash + 1);
|
|
||||||
|
|
||||||
console.log(filename); // "file.txt"
|
|
||||||
```
|
|
||||||
|
|
||||||
### URL-Parameter bereinigen
|
|
||||||
```javascript
|
|
||||||
const url = "https://example.com?id=123&name= Max ";
|
|
||||||
|
|
||||||
if(url.indexOf("?") > -1){
|
|
||||||
const params = url.slice(url.indexOf("?") + 1);
|
|
||||||
console.log(params); // "id=123&name= Max "
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Text durchsuchen (Regex)
|
|
||||||
```javascript
|
|
||||||
const text = "Kontakt: info@example.com oder 089-1234567";
|
|
||||||
|
|
||||||
// E-Mail finden
|
|
||||||
if(text.search(/@/) > -1){
|
|
||||||
console.log("E-Mail vorhanden");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Telefonnummer finden
|
|
||||||
if(text.search(/\d{3}-\d{4}/) > -1){
|
|
||||||
console.log("Telefon vorhanden");
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Häufige Fehler
|
|
||||||
|
|
||||||
### ❌ Falscher Fehler 1: `replace()` ersetzt alle
|
|
||||||
```javascript
|
|
||||||
const text = "aaa";
|
|
||||||
console.log(text.replace("a", "b")); // "baa" – nur erstes!
|
|
||||||
```
|
|
||||||
**Korrekt**:
|
|
||||||
```javascript
|
|
||||||
console.log(text.replace(/a/g, "b")); // "bbb"
|
|
||||||
```
|
|
||||||
|
|
||||||
### ❌ Falscher Fehler 2: `length` ist keine Methode
|
|
||||||
```javascript
|
|
||||||
const text = "Hallo";
|
|
||||||
console.log(text.length()); // TypeError!
|
|
||||||
```
|
|
||||||
**Korrekt**:
|
|
||||||
```javascript
|
|
||||||
console.log(text.length); // 5
|
|
||||||
```
|
|
||||||
|
|
||||||
### ❌ Falscher Fehler 3: Original wird nicht verändert
|
|
||||||
```javascript
|
|
||||||
let text = "hallo";
|
|
||||||
text.toUpperCase();
|
|
||||||
console.log(text); // "hallo" – immer noch klein!
|
|
||||||
```
|
|
||||||
**Korrekt**:
|
|
||||||
```javascript
|
|
||||||
let text = "hallo";
|
|
||||||
text = text.toUpperCase();
|
|
||||||
console.log(text); // "HALLO"
|
|
||||||
```
|
|
||||||
|
|
||||||
### ❌ Falscher Fehler 4: `substr()` verwenden (veraltet)
|
|
||||||
```javascript
|
|
||||||
// Besser nicht:
|
|
||||||
const text = "Hello";
|
|
||||||
console.log(text.substr(0, 3)); // "Hel"
|
|
||||||
|
|
||||||
// Stattdessen:
|
|
||||||
console.log(text.slice(0, 3)); // "Hel"
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Zusammenfassung für die Praxis
|
|
||||||
|
|
||||||
1. **Länge prüfen**: `length`
|
|
||||||
2. **Position suchen**: `indexOf()` oder `lastIndexOf()` (einfach), `search()` (Regex)
|
|
||||||
3. **Substring extrahieren**: `slice()` (mit negativen Indizes), `substring()` (ohne)
|
|
||||||
4. **Ersetzen**: `replace()` mit `g` Flag für alle
|
|
||||||
5. **Groß-/Kleinschreibung**: `toUpperCase()`, `toLowerCase()`
|
|
||||||
6. **Verbinden**: `+` oder Template Literals (nicht `concat()`)
|
|
||||||
7. **Bereinigen**: `trim()` für äußere Whitespaces
|
|
||||||
8. **Einzelnes Zeichen**: `charAt()` oder `[index]`
|
|
||||||
|
|
||||||
**Wichtigste Regel**: Strings sind unveränderlich! Alle Methoden geben einen **neuen String** zurück.
|
|
||||||
11
index.html
11
index.html
@@ -24,6 +24,17 @@
|
|||||||
<div id="message" role="status" aria-live="polite"></div>
|
<div id="message" role="status" aria-live="polite"></div>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
<script src="moneyUtils.js"></script>
|
||||||
<script src="app.js"></script>
|
<script src="app.js"></script>
|
||||||
|
<script>
|
||||||
|
moneyUtils.leseGeldBetrag(' 3,45 € '); // 3.45
|
||||||
|
moneyUtils.leseGeldBetrag('USD 12,345.67'); // 12345.67
|
||||||
|
moneyUtils.entferneZiffernUndSonderzeichen('a1+b2.3, 4!'); // 'a b!'
|
||||||
|
moneyUtils.istDollar('doLLAr 12,50'); // true
|
||||||
|
moneyUtils.istEuro('12.50'); // true (keine Buchstaben)
|
||||||
|
moneyUtils.istEuro('12,50 EUR'); // true
|
||||||
|
moneyUtils.dollarZuEuro(10); // 8.30
|
||||||
|
moneyUtils.leseGeldbetragInEuro('USD 10'); // 8.30
|
||||||
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
130
moneyUtils.js
Normal file
130
moneyUtils.js
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
// moneyUtils.js
|
||||||
|
// Sammlung von Hilfsfunktionen für Geldbeträge (ohne reguläre Ausdrücke)
|
||||||
|
// Die Funktionen werden an window.moneyUtils angehängt.
|
||||||
|
|
||||||
|
(function(global){
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
function toNumberWithTwoDecimals(n){
|
||||||
|
if(!Number.isFinite(n)) return NaN;
|
||||||
|
return Number(n.toFixed(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
// leseGeldBetrag: liest eingabeText und wandelt in Number mit 2 Dezimalstellen um
|
||||||
|
// Erlaubt Komma als Dezimaltrenner (z.B. "3,45" → 3.45)
|
||||||
|
function leseGeldBetrag(eingabeText){
|
||||||
|
if(eingabeText === null || eingabeText === undefined) return NaN;
|
||||||
|
const s = String(eingabeText).trim();
|
||||||
|
|
||||||
|
// Ersetze alle Kommata durch Punkt (ohne Regex: split/join)
|
||||||
|
const withDots = s.split(',').join('.');
|
||||||
|
|
||||||
|
// Baue einen String, der nur aus einer optionalen Vorzeichen, Ziffern und maximal einem Punkt besteht
|
||||||
|
let resultChars = '';
|
||||||
|
let seenDot = false;
|
||||||
|
for(let i = 0; i < withDots.length; i++){
|
||||||
|
const ch = withDots[i];
|
||||||
|
// erlaubte Zeichen: 0-9, '.', '+' und '-' (nur am Anfang)
|
||||||
|
if(ch >= '0' && ch <= '9'){
|
||||||
|
resultChars += ch;
|
||||||
|
} else if(ch === '.'){
|
||||||
|
if(!seenDot){ resultChars += '.'; seenDot = true; }
|
||||||
|
// weitere Punkte ignorieren
|
||||||
|
} else if((ch === '+' || ch === '-') && resultChars.length === 0){
|
||||||
|
// Vorzeichen nur akzeptieren, wenn es das erste Zeichen ist
|
||||||
|
resultChars += ch;
|
||||||
|
} else {
|
||||||
|
// alle anderen Zeichen ignorieren (z.B. Währungen, Buchstaben, Leerzeichen)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(resultChars.length === 0) return NaN;
|
||||||
|
|
||||||
|
const parsed = parseFloat(resultChars);
|
||||||
|
if(!Number.isFinite(parsed)) return NaN;
|
||||||
|
return toNumberWithTwoDecimals(parsed);
|
||||||
|
}
|
||||||
|
|
||||||
|
// entferneZiffernUndSonderzeichen: entfernt Ziffern 0-9, Zeichen + - . , und Leerzeichen
|
||||||
|
function entferneZiffernUndSonderzeichen(eingabeText){
|
||||||
|
if(eingabeText === null || eingabeText === undefined) return '';
|
||||||
|
const s = String(eingabeText);
|
||||||
|
let out = '';
|
||||||
|
for(let i = 0; i < s.length; i++){
|
||||||
|
const ch = s[i];
|
||||||
|
const isDigit = (ch >= '0' && ch <= '9');
|
||||||
|
const isSpecial = (ch === '+' || ch === '-' || ch === '.' || ch === ',' || ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r');
|
||||||
|
if(!isDigit && !isSpecial){
|
||||||
|
out += ch;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hilfsfunktion: prüft, ob der String Buchstaben enthält (a-z oder A-Z)
|
||||||
|
function containsLetters(s){
|
||||||
|
for(let i = 0; i < s.length; i++){
|
||||||
|
const ch = s[i];
|
||||||
|
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// istDollar: true, wenn Text in Number umwandelbar ist und $ / dollar / usd vorkommt (case-insensitive)
|
||||||
|
function istDollar(textEingabe){
|
||||||
|
if(textEingabe === null || textEingabe === undefined) return false;
|
||||||
|
const s = String(textEingabe);
|
||||||
|
const amount = leseGeldBetrag(s);
|
||||||
|
if(!Number.isFinite(amount)) return false;
|
||||||
|
const lower = s.toLowerCase();
|
||||||
|
// '$' check: includes works for single char
|
||||||
|
if(s.indexOf('$') !== -1) return true;
|
||||||
|
if(lower.indexOf('dollar') !== -1) return true;
|
||||||
|
if(lower.indexOf('usd') !== -1) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// istEuro: true, wenn Zahl parsen möglich und 'eur' oder 'euro' vorkommt (case-insensitive)
|
||||||
|
// oder wenn Zahl parsebar und KEINE Buchstaben vorkommen
|
||||||
|
function istEuro(eingabeText){
|
||||||
|
if(eingabeText === null || eingabeText === undefined) return false;
|
||||||
|
const s = String(eingabeText);
|
||||||
|
const amount = leseGeldBetrag(s);
|
||||||
|
if(!Number.isFinite(amount)) return false;
|
||||||
|
const lower = s.toLowerCase();
|
||||||
|
if(lower.indexOf('eur') !== -1) return true;
|
||||||
|
if(lower.indexOf('euro') !== -1) return true;
|
||||||
|
// Wenn keine Buchstaben vorhanden, akzeptiere (z. B. "1234.50")
|
||||||
|
if(!containsLetters(s)) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// dollarZuEuro: Umrechnung (1 USD = 0.83 EUR)
|
||||||
|
function dollarZuEuro(dollarBetrag){
|
||||||
|
const num = Number(dollarBetrag);
|
||||||
|
if(!Number.isFinite(num)) return NaN;
|
||||||
|
return toNumberWithTwoDecimals(num * 0.83);
|
||||||
|
}
|
||||||
|
|
||||||
|
// leseGeldbetragInEuro: liest Betrag und wandelt ggf. von Dollar in Euro um
|
||||||
|
function leseGeldbetragInEuro(eingabeText){
|
||||||
|
const amount = leseGeldBetrag(eingabeText);
|
||||||
|
if(!Number.isFinite(amount)) return NaN;
|
||||||
|
if(istDollar(eingabeText)){
|
||||||
|
return dollarZuEuro(amount);
|
||||||
|
}
|
||||||
|
// sonst: bereits Euro (oder keine Währungsangabe)
|
||||||
|
return toNumberWithTwoDecimals(amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exportiere die Funktionen
|
||||||
|
global.moneyUtils = {
|
||||||
|
leseGeldBetrag,
|
||||||
|
entferneZiffernUndSonderzeichen,
|
||||||
|
istDollar,
|
||||||
|
istEuro,
|
||||||
|
dollarZuEuro,
|
||||||
|
leseGeldbetragInEuro
|
||||||
|
};
|
||||||
|
|
||||||
|
})(window);
|
||||||
170
test_moneyUtils.html
Normal file
170
test_moneyUtils.html
Normal file
@@ -0,0 +1,170 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="de">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||||
|
<title>Test moneyUtils</title>
|
||||||
|
<style>
|
||||||
|
body{
|
||||||
|
font-family:Segoe UI,
|
||||||
|
Roboto,
|
||||||
|
Arial,
|
||||||
|
sans-serif;padding:20px;
|
||||||
|
background:#f6f8fb
|
||||||
|
}
|
||||||
|
.card{
|
||||||
|
background:white;
|
||||||
|
padding:16px;
|
||||||
|
border-radius:8px;
|
||||||
|
max-width:760px;
|
||||||
|
margin:12px auto;
|
||||||
|
box-shadow:0 6px 18px rgba(0,0,0,.06)
|
||||||
|
}
|
||||||
|
label{
|
||||||
|
display:block;
|
||||||
|
margin-top:8px;
|
||||||
|
font-weight:600
|
||||||
|
}
|
||||||
|
input[type=text], input[type=number]{
|
||||||
|
width:100%;
|
||||||
|
padding:8px;
|
||||||
|
margin-top:6px;
|
||||||
|
border:1px solid #ddd;
|
||||||
|
border-radius:4px
|
||||||
|
}
|
||||||
|
button{
|
||||||
|
margin-top:8px;
|
||||||
|
padding:8px 12px;
|
||||||
|
background:#1f6feb;
|
||||||
|
color:white;
|
||||||
|
border:none;
|
||||||
|
border-radius:4px;
|
||||||
|
cursor:pointer
|
||||||
|
}
|
||||||
|
pre{
|
||||||
|
background:#f3f4f6;
|
||||||
|
padding:12px;
|
||||||
|
border-radius:6px;
|
||||||
|
overflow:auto
|
||||||
|
}
|
||||||
|
.row{
|
||||||
|
display:grid;
|
||||||
|
grid-template-columns:1fr 1fr;
|
||||||
|
gap:12px
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<main class="card">
|
||||||
|
<h2>Test für moneyUtils</h2>
|
||||||
|
<p>Diese Seite lädt erlaubt interaktives Testen der Funktionen.</p>
|
||||||
|
|
||||||
|
<label for="inputText">Eingabe (Text)</label>
|
||||||
|
<input id="inputText" type="text" placeholder="z. B. 'USD 12,50' oder ' 3,45 € '" />
|
||||||
|
|
||||||
|
<div class="row">
|
||||||
|
<div>
|
||||||
|
<button id="btnLese">leseGeldBetrag</button>
|
||||||
|
<div><strong>Ergebnis:</strong></div>
|
||||||
|
<pre id="outLese">-</pre>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<button id="btnLeseEuro">leseGeldbetragInEuro</button>
|
||||||
|
<div><strong>Ergebnis:</strong></div>
|
||||||
|
<pre id="outLeseEuro">-</pre>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row">
|
||||||
|
<div>
|
||||||
|
<button id="btnEntferne">entferneZiffernUndSonderzeichen</button>
|
||||||
|
<div><strong>Ergebnis:</strong></div>
|
||||||
|
<pre id="outEntferne">-</pre>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<button id="btnIstDollar">istDollar</button>
|
||||||
|
<div><strong>Ergebnis:</strong></div>
|
||||||
|
<pre id="outIstDollar">-</pre>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row">
|
||||||
|
<div>
|
||||||
|
<button id="btnIstEuro">istEuro</button>
|
||||||
|
<div><strong>Ergebnis:</strong></div>
|
||||||
|
<pre id="outIstEuro">-</pre>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label for="numDollar">Zahl (für dollarZuEuro)</label>
|
||||||
|
<input id="numDollar" type="text" placeholder="z. B. 10 oder 12,5" />
|
||||||
|
<button id="btnDollarZuEuro">dollarZuEuro</button>
|
||||||
|
<div><strong>Ergebnis:</strong></div>
|
||||||
|
<pre id="outDollarZuEuro">-</pre>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<hr />
|
||||||
|
<h3>Beispiele</h3>
|
||||||
|
<p>Klicken Sie einen Beispiel-Button, um das Eingabefeld zu füllen:</p>
|
||||||
|
<div style="display:flex;gap:8px;flex-wrap:wrap">
|
||||||
|
<button class="example">USD 12,50</button>
|
||||||
|
<button class="example">12.50</button>
|
||||||
|
<button class="example"> 3,45 € </button>
|
||||||
|
<button class="example">doLLAr 100</button>
|
||||||
|
<button class="example">500</button>
|
||||||
|
<button class="example">EUR 7,99</button>
|
||||||
|
<button class="example">abc123+-,. def</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p style="margin-top:12px;font-size:0.9rem;color:#444">Hinweis: Diese Tests nutzen die in Ihrem Projekt vorhandene <code>moneyUtils.js</code>. Öffnen Sie diese Seite lokal (z. B. http://localhost:8000/test_moneyUtils.html) im gleichen Verzeichnis.</p>
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<script src="moneyUtils.js"></script>
|
||||||
|
<script>
|
||||||
|
function $(id){ return document.getElementById(id); }
|
||||||
|
|
||||||
|
$('btnLese').addEventListener('click', ()=>{
|
||||||
|
const v = $('inputText').value;
|
||||||
|
const r = window.moneyUtils.leseGeldBetrag(v);
|
||||||
|
$('outLese').textContent = String(r);
|
||||||
|
});
|
||||||
|
|
||||||
|
$('btnLeseEuro').addEventListener('click', ()=>{
|
||||||
|
const v = $('inputText').value;
|
||||||
|
const r = window.moneyUtils.leseGeldbetragInEuro(v);
|
||||||
|
$('outLeseEuro').textContent = String(r);
|
||||||
|
});
|
||||||
|
|
||||||
|
$('btnEntferne').addEventListener('click', ()=>{
|
||||||
|
const v = $('inputText').value;
|
||||||
|
const r = window.moneyUtils.entferneZiffernUndSonderzeichen(v);
|
||||||
|
$('outEntferne').textContent = String(r);
|
||||||
|
});
|
||||||
|
|
||||||
|
$('btnIstDollar').addEventListener('click', ()=>{
|
||||||
|
const v = $('inputText').value;
|
||||||
|
const r = window.moneyUtils.istDollar(v);
|
||||||
|
$('outIstDollar').textContent = String(r);
|
||||||
|
});
|
||||||
|
|
||||||
|
$('btnIstEuro').addEventListener('click', ()=>{
|
||||||
|
const v = $('inputText').value;
|
||||||
|
const r = window.moneyUtils.istEuro(v);
|
||||||
|
$('outIstEuro').textContent = String(r);
|
||||||
|
});
|
||||||
|
|
||||||
|
$('btnDollarZuEuro').addEventListener('click', ()=>{
|
||||||
|
const v = $('numDollar').value;
|
||||||
|
const parsed = window.moneyUtils.leseGeldBetrag(v);
|
||||||
|
const r = window.moneyUtils.dollarZuEuro(parsed);
|
||||||
|
$('outDollarZuEuro').textContent = String(r);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
Array.from(document.querySelectorAll('.example')).forEach(btn => {
|
||||||
|
btn.addEventListener('click', ()=>{ $('inputText').value = btn.textContent; });
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user