Saya ingin mengonversi string berikut ke output yang disediakan.
Input: "\\test\red\bob\fred\new"
Output: "testredbobfrednew"
Aku sudah tidak menemukan solusi yang akan menangani karakter khusus seperti \r, \n, \b, dll
Pada dasarnya saya hanya ingin menyingkirkan apa saja yang tidak alfanumerik. Inilah yang saya coba ...
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"
Satu lagi upaya dengan beberapa langkah
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]/ , "");
}
dengan hasil
Attempt 1: cleanID("\\test\red\bob\fred\new");
Output 1: "BTESTREDOBFREDNEW"
Bantuan apa pun akan dihargai.
Solusi kerja:
Final Attempt 1: return JSON.stringify("\\test\red\bob\fred\new").replace( /\W/g , '');
Output 1: "testredbobfrednew"
var Input = "\\test\red\bob\fred\new"string ini tidak mengandung "merah" sehingga upaya pertama Anda benar, apakah Anda menguji terhadap litteral "\\\\test\\red\\bob\\fred\\new"?
/[^\w\s]+/gicoba ini.