fork(78) download
  1. class Ideone {
  2. static void main(String[] args) {
  3. String specialCharRegex = /[\W_&&[^\s]]/;
  4. println('test 1& test'.replaceAll(specialCharRegex, '\\\\$0'));
  5. println('test 1& test 2$'.replaceAll(specialCharRegex, "\\\\\$0"));
  6. println('test 1& test 2$ test 3%'.replaceAll(specialCharRegex, /\\$0/));
  7. println('!"@#$%&/()=?'.replaceAll(specialCharRegex, /\\$0/));
  8. }
  9. }
  10.  
Success #stdin #stdout 0.93s 83532KB
stdin
Standard input is empty
stdout
test 1\& test
test 1\& test 2\$
test 1\& test 2\$ test 3\%
\!\"\@\#\$\%\&\/\(\)\=\?