Remove empty string characters from javascript string

I use regex to remove keystrokes from a string that is entered using the telnet client (node.js) but the expression in the regex expression input.replace(/\[(B|C|D|A)/gm,"");seems to have some weird effects

enter image description here

string is my attachment, this is a snapshot.

How to delete those empty lines which regex puts in the beginning, or is there a better way to write an expression so that they are not created?

here the input line is http://s21.postimg.org/91e01id13/input.png as its line

"[D[A[C[D[B[D[A[B[C[Dhhh
"

press the left arrow key twice and enter helloas "%1B%5BD%1B%5BDhello%0D%0A"aferencodeURIComponent(string);

+4
source share
6 answers

Escape char (0x1B ASCII).

, .

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

JavaScript String.trim(), .

string.trim();


JavaScript String.replace() Regex, :

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


, , . ( , )

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"

Regex :

"[D[A[C[D[B[D[A[B[C[Dhhh\u000A".replace(/\[(B|C|D|A)/gm,"");

"hhh" ( ), ...

Object:

Object("[D[A[C[D[B[D[A[B[C[Dhhh\u000A".replace(/\[(B|C|D|A)/gm,""));

( Chrome Dev console ):

String {0: "h", 1: "h", 2: "h", 3: "↵", length: 4, [[PrimitiveValue]]: "hhh↵"} 


, , ? , , .

+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>
Hide result

char, .

. : ASCII.

"%1B%5BD%1B%5BDhello%0D%0A" no-Ascii:

  • %0D 13, ( \r).

    "\r".charCodeAt(0); // 13
    
  • %0A 10, ( \n).

    "\n".charCodeAt(0); // 10
    
  • %1B 27, Escape ( \x1B \u001B).

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

    /!\ : nodejs, Esc escape-, . ANSI Escape sequence, : console.log("\x1Bc") .

-ASCII char: 0 31:

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

-ASCII char \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
+2

, (, ascii- < 32), . Chrome console.log , , . , , .

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;

, ascii , . , 0, 3, 6, 9 .

+1

String.trim()

trim()The method returns a string without spaces at both ends. trim () does not affect the value of the string itself.

input.trim();

String.prototype.replace ()

replace()the method returns a new line with some or all matches of the pattern replaced by the replacement. A template can be a string or RegExp , and a replacement can be a string or a function that must be called for each match.

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

try

    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>
Run codeHide result
-1
source

All Articles