从javascript字符串中删除空字符串字符

时间:2014-11-29 08:02:40

标签: javascript regex string

我正在使用正则表达式从一个从telnet客户端输入的字符串中删除按键(node.js) 但我的正则表达式input.replace(/\[(B|C|D|A)/gm,"");似乎有一些奇怪的效果

enter image description here

作为我输入的

string是快照。

如何删除正则表达式开头的那些空字符串,或者是否有更好的方法来编写表达式以便它们不会被创建?

这里是输入字符串 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);

6 个答案:

答案 0 :(得分:4)

使用JavaScript的String.trim()方法,它会删除开头和结尾的空格。

string.trim();


将JavaScript的String.replace()方法与Regex一起使用,如下所示:

string.replace(/\s/g,"");


而且,作为最后的选择,你可以敲掉那3个空位。 (虽然这听起来不是一个好的选择,如果开头的空值有所不同)

string.substring(0,2);


最后,如果你真的疯了,你可以试试所有3。

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↵"} 


那么,我对这个问题是如何产生的还是有点困惑?我只能尝试上述解决方案。

答案 1 :(得分:3)



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());
}
&#13;
p {
  margin:0;
}
&#13;
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
  	<title>JS Bin</title>
</head>
<body>
	<pre id="console"></pre>
</body>
</html>
&#13;
&#13;
&#13;

要删除此字符,您必须知道字符的charcode。

识别字符

请参见字符表:Full list of ASCII characters

在你的字符串&#34; %1B%5BD%1B%5BDhello%0D%0A&#34;中,你有树没有Ascii字符:

  • %0D13回程(写\r)。

    "\r".charCodeAt(0); // 13
    
  • %0A10换行(写\n)。

    "\n".charCodeAt(0); // 10
    
  • %1B27 Escape (写\x1B\u001B)。

    "\x1B".charCodeAt(0); // 27
    

    /!\ 注意:在nodejs中, Esc 启用转义序列,请参阅:ANSI Escape sequences,例如:console.log("\x1Bc")清除屏幕您的控制台。

制作正则表达式

将所有no-ASCII char:0替换为31:

input.replace(/[\x00-\x1F]/g,""); // All no-ASCII char : 0 to 31 (hexa: 1F)

替换所有不带\n的无ASCII字符:

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

答案 2 :(得分:3)

箭头键的前缀为Escape char(0x1B ASCII)。

将它添加到模式中,你将是金色的。

var pattern = /\x1B\[([ABCD])/gm;
decodeURIComponent("%1B%5BD%1B%5BDhello%0D%0A").replace(pattern, "")

答案 3 :(得分:1)

您的字符串中可能嵌入了其他控制字符(例如ascii字符&lt; 32)。 Chrome会将console.log视为空位,但它们并非如此。尝试记录原始字符串中那些显然为空的空格的字符编号。

for (var i = 0, len = s.length; i < len; i++) { 
  console.log("char at " + i + " has code " + s.charCodeAt(i));
}

这应该让你看到你需要替换的东西。

例如(来自chrome)

s = String.fromCharCode(3);
console.log(s); // logs as ""
s.length(); //returns 1;

如果在原始字符串上运行上面的循环,您应该能够看到需要替换的字符的ascii代码。在您的输入图像中,看起来您在0,3,6,9等位置有控制字符。

答案 4 :(得分:0)

<强> String.trim()

trim() 方法返回从两端剥去空白的字符串。 trim()不会影响字符串本身的值。

input.trim();

<强> String.prototype.replace()

replace() 方法返回一个新字符串,其中一个或所有匹配的模式由替换替换。模式可以是字符串或 RegExp ,替换可以是字符串或要为每个匹配调用的函数。

input.replace(/\s/g,'');

答案 5 :(得分:-1)

试试这个

&#13;
&#13;
    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)
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
&#13;
&#13;
&#13;