从字符串中删除不是字母数字字符.遇到[\]字符时遇到问题

Bob*_*non 205 javascript regex

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

Input:  "\\test\red\bob\fred\new"
Output: "testredbobfrednew"
Run Code Online (Sandbox Code Playgroud)

我还没有发现,将处理特殊字符,如任何解决方案\r,\n,\b,等.

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

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"
Run Code Online (Sandbox Code Playgroud)

另一个尝试有多个步骤

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]/ , "");
}
Run Code Online (Sandbox Code Playgroud)

结果

Attempt 1: cleanID("\\test\red\bob\fred\new");
Output 1: "BTESTREDOBFREDNEW"
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

工作方案:

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

AD7*_*six 421

删除非字母数字字符

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

input.replace(/\W/g, '')
Run Code Online (Sandbox Code Playgroud)

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

input.replace(/[^0-9a-z]/gi, '')
Run Code Online (Sandbox Code Playgroud)

输入格式错误

由于测试字符串包含各种不是字母数字的转义字符,因此它将删除它们.

字符串中的反斜杠如果需要字面意思,则需要转义:

"\\test\\red\\bob\\fred\\new".replace(/\W/g, '')
"testredbobfrednew" // output
Run Code Online (Sandbox Code Playgroud)

处理格式错误的字符串

如果你无法正确地转义输入字符串(为什么不能?),或者它来自某种不受信任/配置错误的源 - 你可以这样做:

JSON.stringify("\\test\red\bob\fred\new").replace(/\W/g, '')
"testredbobfrednew" // output
Run Code Online (Sandbox Code Playgroud)

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

JSON.stringify("\\test\red\bob\fred\new")
""\\test\red\bob\fred\new""
Run Code Online (Sandbox Code Playgroud)

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

  • ["因为它们通常是变量标识符中合法的字符."](http://www.perlmonks.org/bare/?node_id=347189).问题中没有"_",当然用`[_\W]`(在问题中使用)替换`\ W`或类似的将删除下划线. (12认同)
  • 这不会删除下划线. (10认同)
  • @kylex,这是因为由于某种原因,下划线被认为是字母数字串的一部分 (4认同)

Dem*_*tix 48

所有当前的答案仍然有怪癖,我能想到的最好的事情是:

string.replace(/[^A-Za-z0-9]/g, '');
Run Code Online (Sandbox Code Playgroud)

下面是一个示例,它捕获我可以在键盘上找到的每个键:

var string = '123abcABC-_*(!@#$%^&*()_-={}[]:\"<>,.?/~`';
var stripped = string.replace(/[^A-Za-z0-9]/g, '');
console.log(stripped);
Run Code Online (Sandbox Code Playgroud)

产出:'123abcABC'

  • `input.replace(/\W/g, '')` 留在字符串中的 `_` 中。@Deminetix 是对的 `string.replace(/[^A-Za-z0-9]/g, '');` 效果更好,因为它从字符串中删除了所有非字母数字字符。 (2认同)

Led*_*rub 12

您可以使用\\p{L}\\p{Letter}查找任何语言的字母并\\d查找数字。

\n
str.replace(/[^\\p{L}\\d]/gu, \'\')\n
Run Code Online (Sandbox Code Playgroud)\n

^否定字符集:not \\P{L} and not \\d

\n

标志:

\n
    \n
  • g (global)根据需要执行尽可能多的替换
  • \n
  • u (unicode)识别 Unicode 转义序列(如\\p{L})。
  • \n
\n

示例:\n

\r\n
\r\n
str.replace(/[^\\p{L}\\d]/gu, \'\')\n
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n


Guf*_*ffa 10

问题不在于如何替换字符,问题在于如何输入字符串.

这是只有在输入这是一个反斜杠字符的第一个反斜杠,其他都是控制字符的一部分\r,\b,\f\n.

由于那些反斜杠不是单独的字符,而是写入单个控制字符的符号的一部分,因此不能单独删除它们.即你不能删除反斜杠,\n因为它不是两个单独的字符,它是你编写控制字符LF换行符的方式.

如果您实际上想要将该输入转换为所需的输出,则需要将每个控制字符替换为相应的字母,例如将字符替换\n为该字符n.

要更换你需要使用一个字符集等的控制字符[\r],如\r在正则表达式的特殊含义:

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, '');
Run Code Online (Sandbox Code Playgroud)

演示:http://jsfiddle.net/SAp4W/

  • @BobbyCannon:我添加了代码,它可以获取您的确切输入并生成所需的输出. (2认同)

myr*_*tio 5

你可以尝试这个正则表达式:

value.replace(/[\W_-]/g, '');
Run Code Online (Sandbox Code Playgroud)


Abd*_*hem 5

要将阿拉伯字母与英文字母一起包含,您可以使用:

\n
// Output: \xd9\x86\xd8\xb5\xd8\xb9\xd8\xb1\xd8\xa8\xd9\x8a\n"\xd9\x86$%^&*(\xd8\xb5 \xd8\xb9___\xd8\xb1\xd8\xa8\xd9\x8a".replace(/[^0-9a-z\\u0600-\\u06FF]/gi, '');\n
Run Code Online (Sandbox Code Playgroud)\n