I'm looking for a way to strip tags from a string in the backend using NodeJS. A few answers here have suggested trying node-validator but neither the docs nor any answers explain specifically how to use it.
For instance, I have a string in a variable like this:
INPUT:
Hello there! I am a string but not a very exciting one!var text = '
DESIRED OUTPUT:
var newText = Hello there! I am a string but not a very exciting one!
The node-validator docs have several options, I think the most pertinent is the trim() function:
var check = require('validator').check,
sanitize = require('validator').sanitize
//Validate
check('test@email.com').len(6, 64).isEmail(); //Methods are chainable
check('abc').isInt(); //Throws 'Invalid integer'
check('abc', 'Please enter a number').isInt(); //Throws 'Please enter a number'
check('abcdefghijklmnopzrtsuvqxyz').is(/^[a-z]+$/);
//Sanitize / Filter
var int = sanitize('0123').toInt(); //123
var bool = sanitize('true').toBoolean(); //true
var str = sanitize(' \t\r hello \n').trim(); //'hello'
var str = sanitize('aaaaaaaaab').ltrim('a'); //'b'
var str = sanitize(large_input_str).xss();
var str = sanitize('<a>').entityDecode(); //''
Is it possible to use this to strip tags (as well as classes) from a string?
EDIT: I also have cheerio (essentially jquery) loaded and was trying to use something similar to:
HTML
Hello there! I am a string but not a very exciting one!
JAVASCRIPT
(function() {
var text = $(.select *).each(function() {
var content = $(this).contents();
$(this).replaceWith(content);
}
);
return text;
}
());
But this results in an Hello....' has no method "contents"''Object ' error, I'm open to using a similar function if it's easier with jQuery.