var x = 't.\\a'; var z = new RegExp( x.replace( /(?=\W)/g, '\\' ), 'g' ); alert( '1t.\\a1-1t.\\a1-1t.\\a1'.replace( z, '' ) );