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

4
我正在使用正则表达式从一个由telnet客户端输入的字符串中删除按键(在node.js中实现),但是我的正则表达式似乎有一些奇怪的影响。

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);之后。

2
input 变量的值是多少? - Avinash Raj
请展示encodeURIComponent(string)的输出;并不是所有的非ASCII字符都能在控制台中显示... - dandavis
k,已在@dandavis处更新。 - Jim Jones
1
好的,我想我们现在都可以看到如何使用类似于 decodeURIComponent(encodeURIComponent(string).replace(/xxx/,yyy)); 的东西。在正则表达式中,还可以使用“-”运算符在两个代码之间支持字符范围。 - dandavis
6个回答

5
使用JavaScript的String.trim()方法,可以去除字符串开头和结尾的空格。
string.trim();

使用JavaScript的String.replace()方法与正则表达式,如下所示:
string.replace(/\s/g,"");

作为最后一种选择,您可以删除那3个空格。(虽然这不是一个好的选择,如果开头的空值不同)
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

3

箭头键以Escape字符(0x1B ASCII)作为前缀。

将其添加到模式中,您就可以轻松使用了。

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

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());
}
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

1

看起来您的字符串中可能嵌入了其他控制字符(例如 ASCII 字符 < 32)。Chrome 将会将它们作为空位置打印在控制台上,但实际上它们并不是空的。尝试打印这些原本空白位置的字符编码。

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等处。

0

String.trim()

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

input.trim();

String.prototype.replace()

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

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

我并不是在试图移除空格,而是在试图移除空字符串。 - Jim Jones

-1

试试这个

    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>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接