从字符串中删除非字母数字字符 [英] Remove not alphanumeric characters from string

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

问题描述

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

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"

另一步尝试多个步骤

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"

推荐答案

删除非字母数字字符

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

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天全站免登陆