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

查看:135
本文介绍了从字符串中删除不是字母数字字符。遇到[\]字符时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将以下字符串转换为提供的输出。

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

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

我找不到任何可以处理特殊字符的解决方案,例如 \ r \ n \ b 等。

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

基本上我只是想摆脱任何不是字母数字的东西。这是我尝试过的......

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

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"

另一个包含多个步骤的尝试

One other attempt with multiple steps

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"

任何帮助都将不胜感激。

Any help would be appreciated.

工作解决方案:

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


推荐答案

删除非字母数字字符



以下是从输入字符串中删除非字母数字字符的正确正则表达式:

Removing non-alphanumeric chars

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

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

请注意 \W 相当于 [^ 0-9a-zA-Z _] - 它包括下划线字符。要删除下划线,请使用例如:

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\red\bob\fred\new").replace(/\W/g, '')
"testredbobfrednew" // output

请注意字符串的json表示包括引号:

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

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

但它们也被替换正则表达式删除。

But they are also removed by the replacement regex.

这篇关于从字符串中删除不是字母数字字符。遇到[\]字符时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆