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
618 views
in Technique[技术] by (71.8m points)

javascript - 从字符串中删除不是字母数字字符。遇到[]字符时遇到问题(Remove not alphanumeric characters from string. Having trouble with the [] character)

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 \r , \n , \b , etc.

(我没有找到任何可以处理\r\n\b \n等特殊字符的解决方案。)

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"
  ask by Bobby Cannon translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
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.

(注意\W相当于[^0-9a-zA-Z_] - 它包含下划线字符。)

To also remove underscores use eg:

(要删除下划线,请使用例如:)

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表示包括引号:)

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

...