Voglio convertire la seguente stringa nell'output fornito.
Input: "\\test\red\bob\fred\new"
Output: "testredbobfrednew"
Non ho trovato alcuna soluzione che consente di gestire caratteri speciali come \r, \n, \b, etc.
Fondamentalmente voglio solo sbarazzarmi di tutto ciò che non è alfanumerico. Ecco cosa ho provato ...
Attempt 1: "\\test\red\bob\fred\new".replace(/[_\W]+/g, "");
Output 1: "testedobredew"
Attempt 2: "\\test\red\bob\fred\new".replace(/['`~!@#$%^&*()_|+-=?;:'",.<>\{\}\[\]\\\/]/gi, "");
Output 2: "testedobred [newline] ew"
Attempt 3: "\\test\red\bob\fred\new".replace(/[^a-zA-Z0-9]/, "");
Output 3: "testedobred [newline] ew"
Attempt 4: "\\test\red\bob\fred\new".replace(/[^a-z0-9\s]/gi, '');
Output 4: "testedobred [newline] ew"
Un altro tentativo con più passaggi
function cleanID(id) {
id = id.toUpperCase();
id = id.replace( /\t/ , "T");
id = id.replace( /\n/ , "N");
id = id.replace( /\r/ , "R");
id = id.replace( /\b/ , "B");
id = id.replace( /\f/ , "F");
return id.replace( /[^a-zA-Z0-9]/ , "");
}
con risultati
Attempt 1: cleanID("\\test\red\bob\fred\new");
Output 1: "BTESTREDOBFREDNEW"
Qualsiasi aiuto sarebbe apprezzato.
Soluzione di lavoro:
Final Attempt 1: return JSON.stringify("\\test\red\bob\fred\new").replace( /\W/g , '');
Output 1: "testredbobfrednew"
var Input = "\\test\red\bob\fred\new"questa stringa non contiene "rosso", quindi il tuo primo tentativo è corretto, stai testando contro la lettera "\\\\test\\red\\bob\\fred\\new"?
/[^\w\s]+/giprova questo.