I’m am having difficulty using the replaceAll method to replace square brackets and double quotes. Any ideas?
Edit:
So far I’ve tried:
replace("\[", "some_thing") // returns illegal escape character
replace("[[", "some_thing") // returns Unclosed character class
replace("^[", "some_thing") // returns Unclosed character class
Don’t use
replaceAll, usereplace. The former uses regular expressions, and[]are special characters within a regex.The double quote is special in Java so you need to escape it with a single backslash (
"\"").If you want to use a regex you need to escape those characters and put them in a character class. A character class is surrounded by
[]and escaping a character is done by preceding it with a backslash\. However, because a backslash is also special in Java, it also needs to be escaped, and so to give the regex engine a backslash you have to use two backslashes (\\[).In the end it should look like this (if you were to use regex):