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)
但它们也被替换正则表达式删除.
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'
Led*_*rub 12
您可以使用\\p{L}
或\\p{Letter}
查找任何语言的字母并\\d
查找数字。
str.replace(/[^\\p{L}\\d]/gu, \'\')\n
Run Code Online (Sandbox Code Playgroud)\n^
否定字符集:not \\P{L} and not \\d
标志:
\ng (global)
根据需要执行尽可能多的替换u (unicode)
识别 Unicode 转义序列(如\\p{L}
)。示例:\n
str.replace(/[^\\p{L}\\d]/gu, \'\')\n
Run Code Online (Sandbox Code Playgroud)\r\nGuf*_*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)
要将阿拉伯字母与英文字母一起包含,您可以使用:
\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