1

The data is actually coming from a websocket and I want to extract the 1234 from it...

var data = 'TEST|1234\0'
console.log(data);

Output: 
TEST|1234

As a result regex isn't working as expected either.

var regex = /\|(.*?)\\/;
var re = new RegExp(regex, "g");
var extractedNumber = msg.match(re);
console.log(extractedNumber);

Output:

null

But if I change the regex to see what is happening I see that \0 is becoming \u0000

var regex = /\|(.*)/;
var re = new RegExp(regex, "g");
var extractedNumber = msg.match(re);
console.log(extractedNumber);

Output:

[ '|1234\u0000' ]

This answer says that \u0000 would be the unicode representation of a character with ASCII code 0.

https://stackoverflow.com/a/12195709/6926996

user630702
  • 2,529
  • 5
  • 35
  • 98
  • Don't know if that's your problem, but just know that when you declare `var data = 'TEST|1234\0'`, the end string does not actually contain a "\". It escapes the `0` to treat it as an ascii code. If you want it to contain a "\", you need to escape it: `var data = 'TEST|1234\\0'` – blex Jun 19 '20 at 14:19
  • I don't think we can escape it either.. since in order to escape I'd first need to store it in a variable. The data is coming from a websocket. – user630702 Jun 19 '20 at 14:24
  • yea that's right. it is in `TEXT|NUMBER\0` and I want to extract the number – user630702 Jun 20 '20 at 09:22

1 Answers1

1

You don't have to use regex if you know the pattern,

var data = 'TEST|1234\0'
console.log(+(data.slice(data.indexOf('|') +1, data.indexOf('\\'))))

+ for convert it to number, slice from '|' +1 until \ (have to write twice \\ at the indexOf ...)

Hagai Harari
  • 2,767
  • 3
  • 11
  • 23