/ 猿问

# 从字符串中删除非字母数字字符。遇到[\]字符

2019-11-05 16:34:58

Input:  "\\test\red\bob\fred\new"

Output: "testredbobfrednew"

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"

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]/ , "");

}

Attempt 1: cleanID("\\test\red\bob\fred\new");

Output 1: "BTESTREDOBFREDNEW"

Final Attempt 1: return JSON.stringify("\\test\red\bob\fred\new").replace( /\W/g , '');

Output 1: "testredbobfrednew"

## 3 回答

input.replace(/\W/g, '')

input.replace(/[^0-9a-z]/gi, '')

"\\test\\red\\bob\\fred\\new".replace(/\W/g, '')

"testredbobfrednew" // output

JSON.stringify("\\test\red\bob\fred\new").replace(/\W/g, '')

"testredbobfrednew" // output

JSON.stringify("\\test\red\bob\fred\new")

""\\test\red\bob\fred\new""

string.replace(/[^A-Za-z0-9]/g, '');

var string = '123abcABC-_*(!@#\$%^&*()_-={}[]:\"<>,.?/~`';

var stripped = string.replace(/[^A-Za-z0-9]/g, '');

console.log(stripped);

var input = "\\test\red\bob\fred\new";

var output = input

.replace(/[\r]/g, 'r')

.replace(/[\b]/g, 'b')

.replace(/[\f]/g, 'f')

.replace(/[\n]/g, 'n')

.replace(/\\/g, '');

• 3 回答
• 0 关注
• 257 浏览

0/150