so I have a long string: "§aSomething, §bthat this §csomething else" and it's dynamic, I want to know how I can replace all of those "§a" or whatever the number is to nothing.
Asked
Active
Viewed 250 times
0
-
"§aSomething, §bthat this §csomething else".replace("§a","") – error404 Jul 17 '17 at 16:54
-
okay I guess that's the only way then. – Aditya Tripathi Jul 17 '17 at 16:55
-
@AnamulHasan This will only replace the first occurrence of `"§a"` – Andreas Jul 17 '17 at 16:57
-
5Possible duplicate of [How to replace all occurrences of a string in JavaScript?](https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript) – Andreas Jul 17 '17 at 16:58
2 Answers
1
Use regular expression
"§aSomething, §bthat this §csomething else".replace(/§./g,"")
.
= Any single character
g
= Global (search on entire string)

Christian Esperar
- 528
- 5
- 19
-
-
Nope, if it's regex replace then it will check your entire string and replace it – Christian Esperar Jul 17 '17 at 17:01
-
-
-
1
In case you want to increase the number of letters you can also use
var string = "§aSomething, §bthat this §csomething else"
string = string.replace(/[§][a-z]{1}/g, '');
where {1} represents the number of letters after §
Output:
Something, that this something else

Bharath M Shetty
- 30,075
- 6
- 57
- 108