Friday 19 May 2017

How do you correctly detect escape characters in a JSON object in Node.js?

I need to detect escape characters in an API call. I've written a function below that takes in a specific value and validates whether it contains escape characters using a regular expression.

Function:

var checkEscapeChars = function (value){
var containsEscapeChars = /\\[ubfnrtv0'"\\]/g.test(value);
return containsEscapeChars; // returns true when contains escape chars
};

My understanding is that if the value is '\f' (line feed), then the function should return true - but it doesn't it returns false. I believe this is because the regex tester treats '\f' and interpreted '\f' differently.

How would I detect escape characters? (I'm running in Node 4.4.7 (and I can't update)).

Thanks



via aaaidan

No comments:

Post a Comment