如何删除正则表达式在开头放置的空字符串,或者有没有更好的方法编写表达式,以便它们不被创建?
这是输入字符串 http://s21.postimg.org/91e01id13/input.png 作为字符串是这样的。
"[D[A[C[D[B[D[A[B[C[Dhhh
"
在按两次左箭头键并输入
hello
后,看起来是这样的:"%1B%5BD%1B%5BDhello%0D%0A"
,在encodeURIComponent(string);
之后。"[D[A[C[D[B[D[A[B[C[Dhhh
"
hello
后,看起来是这样的:"%1B%5BD%1B%5BDhello%0D%0A"
,在encodeURIComponent(string);
之后。String.trim()
方法,可以去除字符串开头和结尾的空格。string.trim();
String.replace()
方法与正则表达式,如下所示:string.replace(/\s/g,"");
string.substring(0,2);
string.substring(0,2).replace(/\S\s/g,"").trim();
复制字符串后(包括换行符):
"[D[A[C[D[B[D[A[B[C[Dhhh\u000A"
"[D[A[C[D[B[D[A[B[C[Dhhh\u000A".replace(/\[(B|C|D|A)/gm,"");
它按预期返回带有换行符的"hhh"
...
当我们将它放入一个对象中:
Object("[D[A[C[D[B[D[A[B[C[Dhhh\u000A".replace(/\[(B|C|D|A)/gm,""));
我在Chrome的开发者控制台和Firefox的控制台中得到了以下返回值:
String {0: "h", 1: "h", 2: "h", 3: "↵", length: 4, [[PrimitiveValue]]: "hhh↵"}
所以,我有点困惑这个问题是如何产生的?我的建议是尝试以上解决方案。
""
在字符串中占据了一个字符,所以我正在尝试将它们删除。但是 string.trim("")
也没有起作用。 - Jim Jones箭头键以Escape
字符(0x1B ASCII)作为前缀。
将其添加到模式中,您就可以轻松使用了。
var pattern = /\x1B\[([ABCD])/gm;
decodeURIComponent("%1B%5BD%1B%5BDhello%0D%0A").replace(pattern, "")
function print() {
var p = document.createElement("p"),
text = Array.prototype.join.call(arguments, ", ");
p.textContent = text;
document.getElementById("console").appendChild(p);
return text;
}
/*
"\t".charCodeAt(0); //9
"\n".charCodeAt(0); //10
"\r".charCodeAt(0); //13
*/
print(decodeURIComponent("%1B%5BD%1B%5BDhello world%0D%0A").split("").join());
var input = decodeURIComponent("%1B%5BD%1B%5BDhello world%0D%0A");
print("before : " + JSON.stringify(input), input.length);
//before : "\u001b[D\u001b[Dhello world\r\n", 19
input = input.replace(/[\u0000-\u001F](\[(B|C|D|A))?/g,"");
//input = input.replace(/[\u0000-\u001F]/g,"");
print("after : " + JSON.stringify(input), input.length);
//after : "[D[Dhello world", 15
for (var i = 0, text = decodeURIComponent("%1B%5BD%1B%5BDhello world%0D%0A"); i < text.length; i++) {
print("- " + JSON.stringify(text[i]), text[i].charCodeAt());
}
p {
margin:0;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<pre id="console"></pre>
</body>
</html>
要去除此字符,您必须知道字符的charcode。
查看字符表格:ASCII字符完整列表。
在您的字符串“%1B%5BD%1B%5BDhello%0D%0A
”中,您有三个非ASCII字符:
%0D
is 13
, Carriage return (write \r
).
"\r".charCodeAt(0); // 13
%0A
is 10
, is line feed (write \n
).
"\n".charCodeAt(0); // 10
%1B
is 27
, is Escape (write \x1B
or \u001B
).
"\x1B".charCodeAt(0); // 27
/!\ Be careful : In nodejs, Esc enable escape sequence, see : ANSI Escape sequences, for example : console.log("\x1Bc")
clear the screen of your console.
替换所有非ASCII字符: 0到31之间的字符:
input.replace(/[\x00-\x1F]/g,""); // All no-ASCII char : 0 to 31 (hexa: 1F)
替换所有非 ASCII 字符而不包括 \n
:
input.replace(/[\x00-\x09\x0b-\x1F]/g,""); // All no-ASCII char : 0 to 31 (hexa: 1F)
仅替换 \r
, \n
, \x1B
:
input.replace(/[\r\n\x1B]/g,"");
var input = decodeURIComponent("%1B%5BD%1B%5BDhello world%0D%0A");
console.log("before : " + JSON.stringify(input), input.length);
//before : "\u001b[D\u001b[Dhello world\r\n", 19
input = input.replace(/[\u0000-\u001F](\[(B|C|D|A))?/g,"");
//or : input.replace(/[\x1B]\[(B|C|D|A)/gm,""); //"hello world\r\n"
console.log("after : " + JSON.stringify(input), input.length);
//after : "hello world", 11
看起来您的字符串中可能嵌入了其他控制字符(例如 ASCII 字符 < 32)。Chrome 将会将它们作为空位置打印在控制台上,但实际上它们并不是空的。尝试打印这些原本空白位置的字符编码。
for (var i = 0, len = s.length; i < len; i++) {
console.log("char at " + i + " has code " + s.charCodeAt(i));
}
s = String.fromCharCode(3);
console.log(s); // logs as ""
s.length(); //returns 1;
trim()
方法返回从字符串两端去除空格的字符串。trim() 不会影响字符串本身的值。
input.trim();
replace()
方法返回一个新字符串,其中某些或所有模式匹配都被替换为替换字符串。模式可以是字符串或RegExp,替换可以是字符串或用于每个匹配调用的函数。
input.replace(/\s/g,'');
试试这个
var re = /[\r\n\s]/gm;
var str = 'aa \nbb ds\n [ ] fghf fdsk dsdlk \nfd';
var subst = '';
var result = str.replace(re, subst);
alert(result)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
input
变量的值是多少? - Avinash Raj