Jim Jones
Jim Jones

Reputation: 2709

Remove empty string characters from a javascript string

I'm using regex to remove keypresses from a string thats being inputed from a telnet client (node.js) but my regex expression, input.replace(/\[(B|C|D|A)/gm,""); seems to be having some weird effects

enter image description here

string being my input is that snapshot.

How do I remove those empty strings regex is putting at the beginning or is there a better way to write the expression so that they aren't created?

here's the input string http://s21.postimg.org/91e01id13/input.png as a string its

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

hitting the left arrow key twice and typing hello looks like this "%1B%5BD%1B%5BDhello%0D%0A" afer encodeURIComponent(string);

Upvotes: 4

Views: 11315

Answers (6)

mleko
mleko

Reputation: 12233

Arrow keys are prefixed with Escape char(0x1B ASCII).

Add it to pattern and You will be golden.

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

Upvotes: 3

Ahosan Karim Asik
Ahosan Karim Asik

Reputation: 3299

try this

    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>

Upvotes: -1

user2226755
user2226755

Reputation: 13173

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>

To remove this char, you must know the charcode of characters.

Identify the characters

See characters table : Full list of ASCII characters.

In your string "%1B%5BD%1B%5BDhello%0D%0A", you have tree no-Ascii chars :

  • %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.

Make regex

Replace all no-ASCII char : 0 to 31 :

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

Replace all no-ASCII char without \n :

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

Replace only \r, \n, \x1B :

input.replace(/[\r\n\x1B]/g,"");

Solution :

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

Upvotes: 3

stevecass
stevecass

Reputation: 141

It looks like you may have other control characters (e.g. ascii characters < 32) embedded in your string. Chrome will console.log them as if they were empty positions, but they are not really. Try logging the character numbers of those apparently empty spaces in your original string.

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

That should let you see what you need to replace.

For example (from chrome)

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

If you run the loop above on your original string, you should be able to see the ascii codes of the characters you need to replace. In your input image it looks like you have there are control characters at positions 0, 3, 6, 9 among others.

Upvotes: 1

user3117575
user3117575

Reputation:

Using JavaScript's String.trim() method, it removes empty spaces at the beginning and end .

string.trim();


Using JavaScript's String.replace() method with Regex, like so:

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


And, as a final alternative, you could just knock off those 3 empty spaces. (Although this doesn't sound like a good alternative, if the empty values at the beginning vary)

string.substring(0,2);


Lastly, if you're feeling really crazy, you could try all 3.

string.substring(0,2).replace(/\S\s/g,"").trim();

After reproducing the string (with the line feed):

"[D[A[C[D[B[D[A[B[C[Dhhh\u000A"

I tried your Regex on the string:

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

It returns "hhh" (with line feed) as expected...

When we throw it into an Object:

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

I get a return of this (in Chrome's Dev console and Firefox's console):

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


So, I'm still a bit confused on how the problem is being produced? All I can say is to try the solutions above.

Upvotes: 5

Prateek
Prateek

Reputation: 6965

String.trim()

trim() method returns the string stripped of whitespace from both ends. trim() does not affect the value of the string itself.

input.trim();

String.prototype.replace()

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

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

Upvotes: 0

Related Questions