Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
114 views
in Technique[技术] by (71.8m points)

javascript - Remove not alphanumeric characters from string

I want to convert the following string to the provided output.

Input:  "\test
edobfred
ew"
Output: "testredbobfrednew"

I've not found any solution that will handle special characters like , , , etc.

Basically I just want to get rid of anything that is not alphanumeric. Here is what I've tried...

Attempt 1: "\test
edobfred
ew".replace(/[_W]+/g, "");
Output 1:  "testedobredew"

Attempt 2: "\test
edobfred
ew".replace(/['`~!@#$%^&*()_|+-=?;:'",.<>{}[]\/]/gi, "");
Output 2:  "testedobred [newline] ew"

Attempt 3: "\test
edobfred
ew".replace(/[^a-zA-Z0-9]/, "");
Output 3:  "testedobred [newline] ew"

Attempt 4: "\test
edobfred
ew".replace(/[^a-z0-9s]/gi, '');
Output 4:  "testedobred [newline] ew"

One other attempt with multiple steps

function cleanID(id) {
    id = id.toUpperCase();
    id = id.replace( // , "T");
    id = id.replace( /
/ , "N");
    id = id.replace( /
/ , "R");
    id = id.replace( // , "B");
    id = id.replace( /f/ , "F");
    return id.replace( /[^a-zA-Z0-9]/ , "");
}

with results

Attempt 1: cleanID("\test
edobfred
ew");
Output 1: "BTESTREDOBFREDNEW"

Any help would be appreciated.

Working Solution:

Final Attempt 1: return JSON.stringify("\test
edobfred
ew").replace( /W/g , '');
Output 1: "testredbobfrednew"
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Removing non-alphanumeric chars

The following is the/a correct regex to strip non-alphanumeric chars from an input string:

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

Note that W is the equivalent of [^0-9a-zA-Z_] - it includes the underscore character. To also remove underscores use e.g.:

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

The input is malformed

Since the test string contains various escaped chars, which are not alphanumeric, it will remove them.

A backslash in the string needs escaping if it's to be taken literally:

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

Handling malformed strings

If you're not able to escape the input string correctly (why not?), or it's coming from some kind of untrusted/misconfigured source - you can do something like this:

JSON.stringify("\test
edobfred
ew").replace(/W/g, '')
"testredbobfrednew" // output

Note that the json representation of a string includes the quotes:

JSON.stringify("\test
edobfred
ew")
""\test
edobfred
ew""

But they are also removed by the replacement regex.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...