最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - Remove empty string characters from a javascript string - Stack Overflow

programmeradmin3浏览0评论

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

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 .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);

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

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/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);

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Nov 29, 2014 at 8:02 Jim JonesJim Jones 2,7098 gold badges28 silver badges44 bronze badges 4
  • 2 What's the value of input variable? – Avinash Raj Commented Nov 29, 2014 at 8:06
  • please show us the output from encodeURIComponent(string); not all non-ascii chars are visible in the console... – dandavis Commented Dec 7, 2014 at 1:06
  • k, updated it @dandavis – Jim Jones Commented Dec 7, 2014 at 1:14
  • 1 well, i think we can all now see how to use something like decodeURIComponent(encodeURIComponent(string).replace(/xxx/,yyy)); there is also char range support in RegExp using the "-" operator between two codes. – dandavis Commented Dec 7, 2014 at 1:26
Add a ment  | 

6 Answers 6

Reset to default 5

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.

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, "")

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

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.

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,'');

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./ajax/libs/jquery/1.11.1/jquery.min.js"></script>

发布评论

评论列表(0)

  1. 暂无评论